浏览代码

skip rendering on error

euromark 12 年之前
父节点
当前提交
6678ced674
共有 2 个文件被更改,包括 34 次插入2 次删除
  1. 24 0
      Test/Case/View/AjaxViewTest.php
  2. 10 2
      View/AjaxView.php

+ 24 - 0
Test/Case/View/AjaxViewTest.php

@@ -85,4 +85,28 @@ class AjaxViewTest extends CakeTestCase {
 		$this->assertTextEquals($expected, $result);
 		$this->assertTextEquals($expected, $result);
 	}
 	}
 
 
+	/**
+	 * AjaxViewTest::testError()
+	 *
+	 * @return void
+	 */
+	public function testError() {
+		$Request = new CakeRequest();
+		$Response = new CakeResponse();
+		$Controller = new Controller($Request, $Response);
+		$items = array(
+			array('title' => 'Title One', 'link' => 'http://example.org/one', 'author' => 'one@example.org', 'description' => 'Content one'),
+			array('title' => 'Title Two', 'link' => 'http://example.org/two', 'author' => 'two@example.org', 'description' => 'Content two'),
+		);
+		$Controller->set(array('error' => 'Some message', 'items' => $items, '_serialize' => array('error', 'items')));
+		$View = new AjaxView($Controller);
+		$View->viewPath = 'Items';
+		$result = $View->render('index');
+
+		$this->assertSame('application/json', $Response->type());
+		$expected = array('error' => 'Some message', 'content' => null, 'items' => $items);
+		$expected = json_encode($expected);
+		$this->assertTextEquals($expected, $result);
+	}
+
 }
 }

+ 10 - 2
View/AjaxView.php

@@ -6,8 +6,10 @@ App::uses('View', 'View');
  *
  *
  * Expects all incoming requests to be of extension "json" and that the expected result
  * Expects all incoming requests to be of extension "json" and that the expected result
  * will also be in JSON format.
  * will also be in JSON format.
- * A valid response will always contain at least "content" and "error" keys.
- * An invalid response may be just HTTP status "code" and error "message" (e.g, on 4xx or 5xx).
+ * A response to an invalid request may be just HTTP status "code" and error "message"
+ * (e.g, on 4xx or 5xx).
+ * A response to a valid request will always contain at least "content" and "error" keys.
+ * You can add more data using _serialize.
  *
  *
  * @author Mark Scherer
  * @author Mark Scherer
  * @license MIT
  * @license MIT
@@ -50,6 +52,8 @@ class AjaxView extends View {
 	 * The rendered content will be part of the JSON response object and
 	 * The rendered content will be part of the JSON response object and
 	 * can be accessed via response.content in JavaScript.
 	 * can be accessed via response.content in JavaScript.
 	 *
 	 *
+	 * If an error has been set, the rendering will be skipped.
+	 *
 	 * @param string $view The view being rendered.
 	 * @param string $view The view being rendered.
 	 * @param string $layout The layout being rendered.
 	 * @param string $layout The layout being rendered.
 	 * @return string The rendered view.
 	 * @return string The rendered view.
@@ -60,6 +64,10 @@ class AjaxView extends View {
 			'content' => null,
 			'content' => null,
 		);
 		);
 
 
+		if (!empty($this->viewVars['error'])) {
+			$view = false;
+		}
+
 		if ($view !== false && $this->_getViewFileName($view)) {
 		if ($view !== false && $this->_getViewFileName($view)) {
 			$response['content'] = parent::render($view, $layout);
 			$response['content'] = parent::render($view, $layout);
 		}
 		}