Merge branch '1.3-jshelper' into 1.3

This commit is contained in:
mark_story 2009-09-20 17:46:30 -04:00
commit e6cc81226d
6 changed files with 116 additions and 17 deletions

View file

@ -240,7 +240,13 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
} }
$options['url'] = $url; $options['url'] = $url;
if (isset($options['update'])) { 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']); unset($options['update']);
} }
$callbacks = array('success', 'error', 'beforeSend', 'complete'); $callbacks = array('success', 'error', 'beforeSend', 'complete');

View file

@ -115,7 +115,7 @@ class JsHelper extends AppHelper {
* @param string $method Method to be called * @param string $method Method to be called
* @param array $params Parameters for the method being called. * @param array $params Parameters for the method being called.
* @access public * @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) { function call__($method, $params) {
if (isset($this->{$this->__engineName}) && method_exists($this->{$this->__engineName}, $method)) { 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); 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 * Writes all Javascript generated so far to a code block or
* caches them to a file and returns a linked script. * caches them to a file and returns a linked script.
@ -203,7 +217,7 @@ class JsHelper extends AppHelper {
/** /**
* Get all the cached scripts * 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. * @return array Array of scripts added to the request.
**/ **/
function getBuffer($clear = true) { function getBuffer($clear = true) {

View file

@ -33,7 +33,7 @@ class PaginatorHelper extends AppHelper {
* *
* @var array * @var array
*/ */
var $helpers = array('Html', 'Ajax'); var $helpers = array('Html');
/** /**
* Holds the default model for paged recordsets * Holds the default model for paged recordsets
@ -42,6 +42,13 @@ class PaginatorHelper extends AppHelper {
*/ */
var $__defaultModel = null; var $__defaultModel = null;
/**
* The class used for 'Ajax' pagination links.
*
* @var string
**/
var $_ajaxHelperClass = 'Js';
/** /**
* Holds the default options for pagination links * Holds the default options for pagination links
* *
@ -66,6 +73,33 @@ class PaginatorHelper extends AppHelper {
*/ */
var $options = array(); 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 * 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); $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(array('page' => $this->current($model)), $url);
$url = array_merge(Set::filter($url, true), array_intersect_key($url, array('plugin'=>true))); $url = array_merge(Set::filter($url, true), array_intersect_key($url, array('plugin'=>true)));
return $this->{$obj}->link($title, $url, $options); return $this->{$obj}->link($title, $url, $options);

View file

@ -160,6 +160,12 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
$expected = '$.ajax({url:"\\/posts\\/view\\/1"});'; $expected = '$.ajax({url:"\\/posts\\/view\\/1"});';
$this->assertEqual($result, $expected); $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( $result = $this->Jquery->request('/people/edit/1', array(
'method' => 'post', 'method' => 'post',
'before' => 'doBefore', 'before' => 'doBefore',
@ -179,7 +185,7 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
'method' => 'post', 'method' => 'post',
'wrapCallbacks' => false '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); $this->assertEqual($result, $expected);
$result = $this->Jquery->request('/people/edit/1', array( $result = $this->Jquery->request('/people/edit/1', array(
@ -190,7 +196,7 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
'data' => '$("#someId").serialize()', 'data' => '$("#someId").serialize()',
'wrapCallbacks' => false '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); $this->assertEqual($result, $expected);
$result = $this->Jquery->request('/people/edit/1', array( $result = $this->Jquery->request('/people/edit/1', array(

View file

@ -419,6 +419,17 @@ CODE;
); );
$this->assertTags($result, $expected); $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);
}
} }

View file

@ -23,7 +23,9 @@
* @lastmodified $Date$ * @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License * @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 * PaginatorHelperTest class
@ -40,7 +42,7 @@ class PaginatorHelperTest extends CakeTestCase {
* @return void * @return void
*/ */
function setUp() { function setUp() {
$this->Paginator = new PaginatorHelper(); $this->Paginator = new PaginatorHelper(array('ajax' => 'Ajax'));
$this->Paginator->params['paging'] = array( $this->Paginator->params['paging'] = array(
'Article' => array( 'Article' => array(
'current' => 9, 'current' => 9,
@ -1682,5 +1684,31 @@ class PaginatorHelperTest extends CakeTestCase {
); );
$this->assertTags($result, $expected); $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'));
}
} }
?> ?>