Skip to content
Permalink
Browse files

Implementing serializeForm() in jquery engine.

  • Loading branch information...
markstory committed Jul 25, 2009
1 parent 76c13fa commit 36722c0ef2d08a4daace0c35126e67c1f495e4b2
Showing with 51 additions and 20 deletions.
  1. +25 −13 cake/libs/view/helpers/jquery_engine.php
  2. +26 −7 cake/tests/cases/libs/view/helpers/jquery_engine.test.php
@@ -50,7 +50,13 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
'direction' => 'orientation'
)
);
/**
* 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.
*
@@ -67,7 +73,6 @@ function _methodTemplate($method, $template, $options, $callbacks) {
$options = $this->_parseOptions($options, $callbacks);
return sprintf($template, $this->selection, $options);
}
/**
* Create javascript selector for a CSS rule
*
@@ -76,13 +81,12 @@ function _methodTemplate($method, $template, $options, $callbacks) {
**/
function get($selector) {
if ($selector == 'window' || $selector == 'document') {
$this->selection = '$(' . $selector .')';
$this->selection = $this->jQueryObject . '(' . $selector .')';
} else {
$this->selection = '$("' . $selector . '")';
$this->selection = $this->jQueryObject . '("' . $selector . '")';
}
return $this;
}
/**
* Add an event to the script cache. Operates on the currently selected elements.
*
@@ -109,7 +113,6 @@ 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
*
@@ -119,7 +122,6 @@ 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.
*
@@ -130,7 +132,6 @@ function domReady($functionBody) {
function each($callback) {
return $this->selection . '.each(function () {' . $callback . '});';
}
/**
* Trigger an Effect.
*
@@ -158,7 +159,6 @@ function effect($name, $options = array()) {
}
return $this->selection . $effect;
}
/**
* Create an $.ajax() call.
*
@@ -183,7 +183,6 @@ function request($url, $options = array()) {
$options = $this->_parseOptions($options, $callbacks);
return '$.ajax({' . $options .'});';
}
/**
* Create a sortable element.
*
@@ -199,7 +198,6 @@ function sortable($options = array()) {
$template = '%s.sortable({%s});';
return $this->_methodTemplate('sortable', $template, $options, $callbacks);
}
/**
* Create a Draggable element
*
@@ -214,7 +212,6 @@ function drag($options = array()) {
$template = '%s.draggable({%s});';
return $this->_methodTemplate('drag', $template, $options, $callbacks);
}
/**
* Create a Droppable element
*
@@ -229,7 +226,6 @@ function drop($options = array()) {
$template = '%s.droppable({%s});';
return $this->_methodTemplate('drop', $template, $options, $callbacks);
}
/**
* Create a Slider element
*
@@ -244,5 +240,21 @@ function slider($options = array()) {
$template = '%s.slider({%s});';
return $this->_methodTemplate('slider', $template, $options, $callbacks);
}
/**
* Serialize the form attached to $selector. If the current selection is not an input or
* form, errors will be created in the Javascript.
*
* Pass `true` for $isForm if the current selection is a form element.
*
* @param boolean $isForm is the current selection a form?
* @return string completed form serialization script
**/
function serializeForm($isForm = false) {
$selector = $this->selection;
if (!$isForm) {
$selector = $this->selection . '.closest("form")';
}
return $selector . '.serialize();';
}
}
?>
@@ -47,15 +47,15 @@ function testSelector() {
$result = $this->Jquery->get('#content');
$this->assertEqual($result, $this->Jquery);
$this->assertEqual($this->Jquery->selection, '$("#content")');
$result = $this->Jquery->get('document');
$this->assertEqual($result, $this->Jquery);
$this->assertEqual($this->Jquery->selection, '$(document)');
$result = $this->Jquery->get('window');
$this->assertEqual($result, $this->Jquery);
$this->assertEqual($this->Jquery->selection, '$(window)');
$result = $this->Jquery->get('ul');
$this->assertEqual($result, $this->Jquery);
$this->assertEqual($this->Jquery->selection, '$("ul")');
@@ -147,7 +147,7 @@ function testRequest() {
$result = $this->Jquery->request('/people/edit/1', array(
'method' => 'post',
'before' => 'doBefore',
'before' => 'doBefore',
'complete' => 'doComplete',
'success' => 'doSuccess',
'error' => 'handleError',
@@ -156,7 +156,7 @@ function testRequest() {
));
$expected = '$.ajax({beforeSend:doBefore, complete:doComplete, data:"name=jim&height=185cm", dataType:"json", error:handleError, method:"post", success:doSuccess, url:"\\/people\\/edit\\/1"});';
$this->assertEqual($result, $expected);
$result = $this->Jquery->request('/people/edit/1', array(
'update' => '#updated',
'success' => 'doFoo',
@@ -192,7 +192,7 @@ function testDrag() {
$result = $this->Jquery->drag(array(
'container' => '#content',
'start' => 'onStart',
'drag' => 'onDrag',
'drag' => 'onDrag',
'stop' => 'onStop',
'snapGrid' => array(10, 10),
));
@@ -208,7 +208,7 @@ function testDrop() {
$this->Jquery->get('#element');
$result = $this->Jquery->drop(array(
'accept' => '.items',
'hover' => 'onHover',
'hover' => 'onHover',
'leave' => 'onExit',
'drop' => 'onDrop'
));
@@ -233,5 +233,24 @@ function testSlider() {
$expected = '$("#element").slider({change:onChange, max:10, min:0, orientation:"vertical", stop:onComplete, value:2});';
$this->assertEqual($result, $expected);
}
/**
* test the serializeForm method
*
* @return void
**/
function testSerializeForm() {
$this->Jquery->get('#element');
$result = $this->Jquery->serializeForm(false);
$expected = '$("#element").closest("form").serialize();';
$this->assertEqual($result, $expected);
$result = $this->Jquery->serializeForm(true);
$expected = '$("#element").serialize();';
$this->assertEqual($result, $expected);
$result = $this->Jquery->serializeForm();
$expected = '$("#element").closest("form").serialize();';
$this->assertEqual($result, $expected);
}
}
?>

0 comments on commit 36722c0

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