Skip to content
Permalink
Browse files

Adding callbackArguments and parseCallbacks to jQuery engine.

  • Loading branch information...
markstory committed Jul 30, 2009
1 parent 24299cf commit 5b13b29a925c4657b6884335c33e21c4a9929cf4
Showing with 125 additions and 6 deletions.
  1. +62 −0 cake/libs/view/helpers/jquery_engine.php
  2. +63 −6 cake/tests/cases/libs/view/helpers/jquery_engine.test.php
@@ -51,13 +51,62 @@ 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.
*
* @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 @@ function get($selector) {
}
return $this;
}
/**
* Add an event to the script cache. Operates on the currently selected elements.
*
@@ -114,6 +166,7 @@ function event($type, $callback, $options = array()) {
}
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 @@ function event($type, $callback, $options = array()) {
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 @@ function domReady($functionBody) {
function each($callback) {
return $this->selection . '.each(function () {' . $callback . '});';
}
/**
* Trigger an Effect.
*
@@ -160,6 +215,7 @@ function effect($name, $options = array()) {
}
return $this->selection . $effect;
}
/**
* Create an $.ajax() call.
*
@@ -185,9 +241,11 @@ function request($url, $options = array()) {
$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 @@ function sortable($options = array()) {
$template = '%s.sortable({%s});';
return $this->_methodTemplate('sortable', $template, $options, $callbacks);
}
/**
* Create a Draggable element
*
@@ -217,6 +276,7 @@ function drag($options = array()) {
$template = '%s.draggable({%s});';
return $this->_methodTemplate('drag', $template, $options, $callbacks);
}
/**
* Create a Droppable element
*
@@ -231,6 +291,7 @@ function drop($options = array()) {
$template = '%s.droppable({%s});';
return $this->_methodTemplate('drop', $template, $options, $callbacks);
}
/**
* Create a Slider element
*
@@ -245,6 +306,7 @@ function slider($options = array()) {
$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.
@@ -152,28 +152,41 @@ function testRequest() {
'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);
$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);
$result = $this->Jquery->request('/people/edit/1', array(
'update' => '#updated',
'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 @@ function testSortable() {
'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 @@ function testDrag() {
'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 @@ function testDrop() {
'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 @@ function testSlider() {
'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

0 comments on commit 5b13b29

Please sign in to comment.
You can’t perform that action at this time.