Skip to content

Commit

Permalink
Implementing serializeForm() in jquery engine.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Jul 25, 2009
1 parent 76c13fa commit 36722c0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 20 deletions.
38 changes: 25 additions & 13 deletions cake/libs/view/helpers/jquery_engine.php
Expand Up @@ -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.
*
Expand All @@ -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
*
Expand All @@ -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.
*
Expand All @@ -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
*
Expand All @@ -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.
*
Expand All @@ -130,7 +132,6 @@ function domReady($functionBody) {
function each($callback) {
return $this->selection . '.each(function () {' . $callback . '});';
}

/**
* Trigger an Effect.
*
Expand Down Expand Up @@ -158,7 +159,6 @@ function effect($name, $options = array()) {
}
return $this->selection . $effect;
}

/**
* Create an $.ajax() call.
*
Expand All @@ -183,7 +183,6 @@ function request($url, $options = array()) {
$options = $this->_parseOptions($options, $callbacks);
return '$.ajax({' . $options .'});';
}

/**
* Create a sortable element.
*
Expand All @@ -199,7 +198,6 @@ function sortable($options = array()) {
$template = '%s.sortable({%s});';
return $this->_methodTemplate('sortable', $template, $options, $callbacks);
}

/**
* Create a Draggable element
*
Expand All @@ -214,7 +212,6 @@ function drag($options = array()) {
$template = '%s.draggable({%s});';
return $this->_methodTemplate('drag', $template, $options, $callbacks);
}

/**
* Create a Droppable element
*
Expand All @@ -229,7 +226,6 @@ function drop($options = array()) {
$template = '%s.droppable({%s});';
return $this->_methodTemplate('drop', $template, $options, $callbacks);
}

/**
* Create a Slider element
*
Expand All @@ -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();';
}
}
?>
33 changes: 26 additions & 7 deletions cake/tests/cases/libs/view/helpers/jquery_engine.test.php
Expand Up @@ -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")');
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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),
));
Expand All @@ -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'
));
Expand All @@ -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.