diff --git a/cake/libs/view/helpers/jquery_engine.php b/cake/libs/view/helpers/jquery_engine.php index 476327b91..b806794f0 100644 --- a/cake/libs/view/helpers/jquery_engine.php +++ b/cake/libs/view/helpers/jquery_engine.php @@ -160,7 +160,7 @@ class JqueryEngineHelper extends JsBaseEngineHelper { function event($type, $callback, $options = array()) { $defaults = array('wrap' => true, 'stop' => true); $options = array_merge($defaults, $options); - + $function = 'function (event) {%s}'; if ($options['wrap'] && $options['stop']) { $callback .= "\nreturn false;"; @@ -240,7 +240,13 @@ class JqueryEngineHelper extends JsBaseEngineHelper { } $options['url'] = $url; if (isset($options['update'])) { - $options['success'] = 'function (msg, status) {$("' . $options['update'] . '").html(msg);}'; + $wrapCallbacks = isset($options['wrapCallbacks']) ? $options['wrapCallbacks'] : true; + if ($wrapCallbacks) { + $success = '$("' . $options['update'] . '").html(data);'; + } else { + $success = 'function (data, textStatus) {$("' . $options['update'] . '").html(data);}'; + } + $options['success'] = $success; unset($options['update']); } $callbacks = array('success', 'error', 'beforeSend', 'complete'); @@ -269,7 +275,7 @@ class JqueryEngineHelper extends JsBaseEngineHelper { /** * Create a Draggable element - * + * * Requires both Ui.Core and Ui.Draggable to be loaded. * * @param array $options Array of options for the draggable element. @@ -283,7 +289,7 @@ class JqueryEngineHelper extends JsBaseEngineHelper { /** * Create a Droppable element - * + * * Requires both Ui.Core and Ui.Droppable to be loaded. * * @param array $options Array of options for the droppable element. @@ -297,7 +303,7 @@ class JqueryEngineHelper extends JsBaseEngineHelper { /** * Create a Slider element - * + * * Requires both Ui.Core and Ui.Slider to be loaded. * * @param array $options Array of options for the droppable element. @@ -311,9 +317,9 @@ class JqueryEngineHelper extends JsBaseEngineHelper { } /** - * Serialize a form attached to $selector. If the current selection is not an input or + * Serialize a form attached to $selector. If the current selection is not an input or * form, errors will be created in the Javascript. - * + * * @param array $options Options for the serialization * @return string completed form serialization script * @see JsHelper::serializeForm() for option list. diff --git a/cake/libs/view/helpers/js.php b/cake/libs/view/helpers/js.php index d0f8844ab..0bf225de5 100644 --- a/cake/libs/view/helpers/js.php +++ b/cake/libs/view/helpers/js.php @@ -115,7 +115,7 @@ class JsHelper extends AppHelper { * @param string $method Method to be called * @param array $params Parameters for the method being called. * @access public - * @return mixed + * @return mixed Depends on the return of the dispatched method, or it could be an instance of the EngineHelper **/ function call__($method, $params) { if (isset($this->{$this->__engineName}) && method_exists($this->{$this->__engineName}, $method)) { @@ -150,6 +150,20 @@ class JsHelper extends AppHelper { trigger_error(sprintf(__('JsHelper:: Missing Method %s is undefined', true), $method), E_USER_WARNING); } +/** + * Workaround for Object::Object() existing. Since Object::object exists, it does not + * fall into call__ and is not passed onto the engine helper. See JsBaseEngineHelper::object() for + * more information on this method. + * + * @param mixed $data Data to convert into JSON + * @param array $options Options to use for encoding JSON. See JsBaseEngineHelper::object() for more details. + * @return string encoded JSON + * @deprecated Remove when support for PHP4 and Object::object are removed. + **/ + function object($data = array(), $options = array()) { + return $this->{$this->__engineName}->object($data, $options); + } + /** * Writes all Javascript generated so far to a code block or * caches them to a file and returns a linked script. @@ -203,7 +217,7 @@ class JsHelper extends AppHelper { /** * Get all the cached scripts * - * @param boolean $clear Whether or not to clear the script caches + * @param boolean $clear Whether or not to clear the script caches (default true) * @return array Array of scripts added to the request. **/ function getBuffer($clear = true) { @@ -919,10 +933,10 @@ class JsBaseEngineHelper extends AppHelper { } /** - * Prepare callbacks and wrap them with function ([args]) { } as defined in + * Prepare callbacks and wrap them with function ([args]) { } as defined in * _callbackArgs array. * - * @param string $method Name of the method you are preparing callbacks for. + * @param string $method Name of the method you are preparing callbacks for. * @param array $options Array of options being parsed * @param string $callbacks Additional Keys that contain callbacks * @access protected diff --git a/cake/libs/view/helpers/paginator.php b/cake/libs/view/helpers/paginator.php index 847adbf17..1cc614532 100644 --- a/cake/libs/view/helpers/paginator.php +++ b/cake/libs/view/helpers/paginator.php @@ -33,7 +33,7 @@ class PaginatorHelper extends AppHelper { * * @var array */ - var $helpers = array('Html', 'Ajax'); + var $helpers = array('Html'); /** * Holds the default model for paged recordsets @@ -42,6 +42,13 @@ class PaginatorHelper extends AppHelper { */ var $__defaultModel = null; +/** + * The class used for 'Ajax' pagination links. + * + * @var string + **/ + var $_ajaxHelperClass = 'Js'; + /** * Holds the default options for pagination links * @@ -66,6 +73,33 @@ class PaginatorHelper extends AppHelper { */ var $options = array(); +/** + * Constructor for the helper. Sets up the helper that is used for creating 'AJAX' links. + * + * Use `var $helpers = array('Paginator' => array('ajax' => 'CustomHelper'));` to set a custom Helper + * or choose a non JsHelper Helper. If you want to use a specific library with JsHelper declare JsHelper and its + * adapter before including PaginatorHelper in your helpers array. + * + * The chosen custom helper must implement a `link()` method. + * + * @return void + **/ + function __construct($config = array()) { + $ajaxProvider = isset($config['ajax']) ? $config['ajax'] : 'Js'; + $this->helpers[] = $ajaxProvider; + $this->_ajaxHelperClass = $ajaxProvider; + + App::import('Helper', $ajaxProvider); + $classname = $ajaxProvider . 'Helper'; + if (!method_exists($classname, 'link')) { + $message = sprintf( + __('%s does not implement a link() method, it is incompatible with PaginatorHelper', true), + $classname + ); + trigger_error($message, E_USER_WARNING); + } + } + /** * Gets the current paging parameters from the resultset for the given model * @@ -280,7 +314,7 @@ class PaginatorHelper extends AppHelper { } $url = $this->url($url, true, $model); - $obj = isset($options['update']) ? 'Ajax' : 'Html'; + $obj = isset($options['update']) ? $this->_ajaxHelperClass : 'Html'; $url = array_merge(array('page' => $this->current($model)), $url); $url = array_merge(Set::filter($url, true), array_intersect_key($url, array('plugin'=>true))); return $this->{$obj}->link($title, $url, $options); diff --git a/cake/tests/cases/libs/view/helpers/jquery_engine.test.php b/cake/tests/cases/libs/view/helpers/jquery_engine.test.php index 65194a7df..23d237cb0 100644 --- a/cake/tests/cases/libs/view/helpers/jquery_engine.test.php +++ b/cake/tests/cases/libs/view/helpers/jquery_engine.test.php @@ -160,6 +160,12 @@ class JqueryEngineHelperTestCase extends CakeTestCase { $expected = '$.ajax({url:"\\/posts\\/view\\/1"});'; $this->assertEqual($result, $expected); + $result = $this->Jquery->request(array('controller' => 'posts', 'action' => 'view', 1), array( + 'update' => '#content' + )); + $expected = '$.ajax({success:function (data, textStatus) {$("#content").html(data);}, url:"\/posts\/view\/1"});'; + $this->assertEqual($result, $expected); + $result = $this->Jquery->request('/people/edit/1', array( 'method' => 'post', 'before' => 'doBefore', @@ -179,7 +185,7 @@ class JqueryEngineHelperTestCase extends CakeTestCase { 'method' => 'post', 'wrapCallbacks' => false )); - $expected = '$.ajax({success:function (msg, status) {$("#updated").html(msg);}, type:"post", url:"\\/people\\/edit\\/1"});'; + $expected = '$.ajax({success:function (data, textStatus) {$("#updated").html(data);}, type:"post", url:"\\/people\\/edit\\/1"});'; $this->assertEqual($result, $expected); $result = $this->Jquery->request('/people/edit/1', array( @@ -190,7 +196,7 @@ class JqueryEngineHelperTestCase extends CakeTestCase { 'data' => '$("#someId").serialize()', 'wrapCallbacks' => false )); - $expected = '$.ajax({data:$("#someId").serialize(), success:function (msg, status) {$("#updated").html(msg);}, type:"post", url:"\\/people\\/edit\\/1"});'; + $expected = '$.ajax({data:$("#someId").serialize(), success:function (data, textStatus) {$("#updated").html(data);}, type:"post", url:"\\/people\\/edit\\/1"});'; $this->assertEqual($result, $expected); $result = $this->Jquery->request('/people/edit/1', array( diff --git a/cake/tests/cases/libs/view/helpers/js.test.php b/cake/tests/cases/libs/view/helpers/js.test.php index 972e056af..61be3a0b2 100644 --- a/cake/tests/cases/libs/view/helpers/js.test.php +++ b/cake/tests/cases/libs/view/helpers/js.test.php @@ -419,6 +419,17 @@ CODE; ); $this->assertTags($result, $expected); } + +/** + * Test that Object::Object() is not breaking json output in JsHelper + * + * @return void + **/ + function testObjectPassThrough() { + $result = $this->Js->object(array('one' => 'first', 'two' => 'second')); + $expected = '{"one":"first","two":"second"}'; + $this->assertEqual($result, $expected); + } } diff --git a/cake/tests/cases/libs/view/helpers/paginator.test.php b/cake/tests/cases/libs/view/helpers/paginator.test.php index b5ba12e87..7904b5a1d 100644 --- a/cake/tests/cases/libs/view/helpers/paginator.test.php +++ b/cake/tests/cases/libs/view/helpers/paginator.test.php @@ -23,7 +23,9 @@ * @lastmodified $Date$ * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License */ -App::import('Helper', array('Html', 'Paginator', 'Form', 'Ajax', 'Javascript')); +App::import('Helper', array('Html', 'Paginator', 'Form', 'Ajax', 'Javascript', 'Js')); + +Mock::generate('JsHelper', 'PaginatorMockJsHelper'); /** * PaginatorHelperTest class @@ -40,7 +42,7 @@ class PaginatorHelperTest extends CakeTestCase { * @return void */ function setUp() { - $this->Paginator = new PaginatorHelper(); + $this->Paginator = new PaginatorHelper(array('ajax' => 'Ajax')); $this->Paginator->params['paging'] = array( 'Article' => array( 'current' => 9, @@ -1682,5 +1684,31 @@ class PaginatorHelperTest extends CakeTestCase { ); $this->assertTags($result, $expected); } + +/** + * test that mock classes injected into paginatorHelper are called when using link() + * + * @return void + **/ + function testMockAjaxProviderClassInjection() { + $Paginator =& new PaginatorHelper(array('ajax' => 'PaginatorMockJs')); + $Paginator->params['paging'] = array( + 'Article' => array( + 'current' => 9, + 'count' => 62, + 'prevPage' => false, + 'nextPage' => true, + 'pageCount' => 7, + 'defaults' => array(), + 'options' => array() + ) + ); + $Paginator->PaginatorMockJs =& new PaginatorMockJsHelper(); + $Paginator->PaginatorMockJs->expectOnce('link'); + $result = $Paginator->link('Page 2', array('page' => 2), array('update' => '#content')); + + $this->expectError(); + $Paginator =& new PaginatorHelper(array('ajax' => 'Form')); + } } ?> \ No newline at end of file