Adding callbackArguments and parseCallbacks to jQuery engine.

This commit is contained in:
mark_story 2009-07-29 22:19:02 -04:00
parent 24299cf943
commit 5b13b29a92
2 changed files with 125 additions and 6 deletions

View file

@ -51,6 +51,54 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
'direction' => 'orientation'
)
);
/**
* callback arguments lists
*
* @var string
**/
var $_callbackArguments = array(
'slider' => array(
'start' => 'event, ui',
'slide' => 'event, ui',
'change' => 'event, ui',
'stop' => 'event, ui'
),
'sortable' => array(
'start' => 'event, ui',
'sort' => 'event, ui',
'change' => 'event, ui',
'beforeStop' => 'event, ui',
'stop' => 'event, ui',
'update' => 'event, ui',
'receive' => 'event, ui',
'remove' => 'event, ui',
'over' => 'event, ui',
'out' => 'event, ui',
'activate' => 'event, ui',
'deactivate' => 'event, ui'
),
'drag' => array(
'start' => 'event, ui',
'drag' => 'event, ui',
'stop' => 'event, ui',
),
'drop' => array(
'activate' => 'event, ui',
'deactivate' => 'event, ui',
'over' => 'event, ui',
'out' => 'event, ui',
'drop' => 'event, ui'
),
'request' => array(
'beforeSend' => 'XMLHttpRequest',
'error' => 'XMLHttpRequest, textStatus, errorThrown',
'success' => 'data, textStatus',
'complete' => 'XMLHttpRequest, textStatus',
'xhr' => ''
)
);
/**
* The variable name of the jQuery Object, useful
* when jQuery is put into noConflict() mode.
@ -58,6 +106,7 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
* @var string
**/
var $jQueryObject = '$';
/**
* Helper function to wrap repetitive simple method templating.
*
@ -71,9 +120,11 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
**/
function _methodTemplate($method, $template, $options, $callbacks) {
$options = $this->_mapOptions($method, $options);
$options = $this->_prepareCallbacks($method, $options);
$options = $this->_parseOptions($options, $callbacks);
return sprintf($template, $this->selection, $options);
}
/**
* Create javascript selector for a CSS rule
*
@ -88,6 +139,7 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
}
return $this;
}
/**
* Add an event to the script cache. Operates on the currently selected elements.
*
@ -114,6 +166,7 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
}
return sprintf('%s.bind("%s", %s);', $this->selection, $type, $callback);
}
/**
* Create a domReady event. This is a special event in many libraries
*
@ -123,6 +176,7 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
function domReady($functionBody) {
return $this->get('document')->event('ready', $functionBody, array('stop' => false));
}
/**
* Create an iteration over the current selection result.
*
@ -133,6 +187,7 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
function each($callback) {
return $this->selection . '.each(function () {' . $callback . '});';
}
/**
* Trigger an Effect.
*
@ -160,6 +215,7 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
}
return $this->selection . $effect;
}
/**
* Create an $.ajax() call.
*
@ -185,9 +241,11 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
$callbacks[] = 'data';
unset($options['dataExpression']);
}
$options = $this->_prepareCallbacks('request', $options);
$options = $this->_parseOptions($options, $callbacks);
return '$.ajax({' . $options .'});';
}
/**
* Create a sortable element.
*
@ -203,6 +261,7 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
$template = '%s.sortable({%s});';
return $this->_methodTemplate('sortable', $template, $options, $callbacks);
}
/**
* Create a Draggable element
*
@ -217,6 +276,7 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
$template = '%s.draggable({%s});';
return $this->_methodTemplate('drag', $template, $options, $callbacks);
}
/**
* Create a Droppable element
*
@ -231,6 +291,7 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
$template = '%s.droppable({%s});';
return $this->_methodTemplate('drop', $template, $options, $callbacks);
}
/**
* Create a Slider element
*
@ -245,6 +306,7 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
$template = '%s.slider({%s});';
return $this->_methodTemplate('slider', $template, $options, $callbacks);
}
/**
* Serialize a form attached to $selector. If the current selection is not an input or
* form, errors will be created in the Javascript.

View file

@ -152,7 +152,8 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
'success' => 'doSuccess',
'error' => 'handleError',
'type' => 'json',
'data' => array('name' => 'jim', 'height' => '185cm')
'data' => array('name' => 'jim', 'height' => '185cm'),
'wrapCallbacks' => false
));
$expected = '$.ajax({beforeSend:doBefore, complete:doComplete, data:"name=jim&height=185cm", dataType:"json", error:handleError, success:doSuccess, type:"post", url:"\\/people\\/edit\\/1"});';
$this->assertEqual($result, $expected);
@ -160,7 +161,8 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
$result = $this->Jquery->request('/people/edit/1', array(
'update' => '#updated',
'success' => 'doFoo',
'method' => 'post'
'method' => 'post',
'wrapCallbacks' => false
));
$expected = '$.ajax({success:function (msg, status) {$("#updated").html(msg);}, type:"post", url:"\\/people\\/edit\\/1"});';
$this->assertEqual($result, $expected);
@ -170,10 +172,21 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
'success' => 'doFoo',
'method' => 'post',
'dataExpression' => true,
'data' => '$("#someId").serialize()'
'data' => '$("#someId").serialize()',
'wrapCallbacks' => false
));
$expected = '$.ajax({data:$("#someId").serialize(), success:function (msg, status) {$("#updated").html(msg);}, type:"post", url:"\\/people\\/edit\\/1"});';
$this->assertEqual($result, $expected);
$result = $this->Jquery->request('/people/edit/1', array(
'success' => 'doFoo',
'before' => 'doBefore',
'method' => 'post',
'dataExpression' => true,
'data' => '$("#someId").serialize()',
));
$expected = '$.ajax({beforeSend:function (XMLHttpRequest) {doBefore}, data:$("#someId").serialize(), success:function (data, textStatus) {doFoo}, type:"post", url:"\\/people\\/edit\\/1"});';
$this->assertEqual($result, $expected);
}
/**
* test sortable list generation
@ -188,9 +201,20 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
'start' => 'onStart',
'complete' => 'onStop',
'sort' => 'onSort',
'wrapCallbacks' => false
));
$expected = '$("#myList").sortable({containment:"parent", distance:5, sort:onSort, start:onStart, stop:onStop});';
$this->assertEqual($result, $expected);
$result = $this->Jquery->sortable(array(
'distance' => 5,
'containment' => 'parent',
'start' => 'onStart',
'complete' => 'onStop',
'sort' => 'onSort',
));
$expected = '$("#myList").sortable({containment:"parent", distance:5, sort:function (event, ui) {onSort}, start:function (event, ui) {onStart}, stop:function (event, ui) {onStop}});';
$this->assertEqual($result, $expected);
}
/**
* test drag() method
@ -205,9 +229,20 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
'drag' => 'onDrag',
'stop' => 'onStop',
'snapGrid' => array(10, 10),
'wrapCallbacks' => false
));
$expected = '$("#element").draggable({containment:"#content", drag:onDrag, grid:[10,10], start:onStart, stop:onStop});';
$this->assertEqual($result, $expected);
$result = $this->Jquery->drag(array(
'container' => '#content',
'start' => 'onStart',
'drag' => 'onDrag',
'stop' => 'onStop',
'snapGrid' => array(10, 10),
));
$expected = '$("#element").draggable({containment:"#content", drag:function (event, ui) {onDrag}, grid:[10,10], start:function (event, ui) {onStart}, stop:function (event, ui) {onStop}});';
$this->assertEqual($result, $expected);
}
/**
* test drop() method
@ -220,10 +255,20 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
'accept' => '.items',
'hover' => 'onHover',
'leave' => 'onExit',
'drop' => 'onDrop'
'drop' => 'onDrop',
'wrapCallbacks' => false
));
$expected = '$("#element").droppable({accept:".items", drop:onDrop, out:onExit, over:onHover});';
$this->assertEqual($result, $expected);
$result = $this->Jquery->drop(array(
'accept' => '.items',
'hover' => 'onHover',
'leave' => 'onExit',
'drop' => 'onDrop',
));
$expected = '$("#element").droppable({accept:".items", drop:function (event, ui) {onDrop}, out:function (event, ui) {onExit}, over:function (event, ui) {onHover}});';
$this->assertEqual($result, $expected);
}
/**
* test slider generation
@ -238,10 +283,22 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
'min' => 0,
'max' => 10,
'value' => 2,
'direction' => 'vertical'
'direction' => 'vertical',
'wrapCallbacks' => false
));
$expected = '$("#element").slider({change:onChange, max:10, min:0, orientation:"vertical", stop:onComplete, value:2});';
$this->assertEqual($result, $expected);
$result = $this->Jquery->slider(array(
'complete' => 'onComplete',
'change' => 'onChange',
'min' => 0,
'max' => 10,
'value' => 2,
'direction' => 'vertical',
));
$expected = '$("#element").slider({change:function (event, ui) {onChange}, max:10, min:0, orientation:"vertical", stop:function (event, ui) {onComplete}, value:2});';
$this->assertEqual($result, $expected);
}
/**
* test the serializeForm method