Implementing serializeForm() in jquery engine.

This commit is contained in:
mark_story 2009-07-24 22:46:33 -04:00
parent 76c13fa98e
commit 36722c0ef2
2 changed files with 51 additions and 20 deletions

View file

@ -50,7 +50,13 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
'direction' => 'orientation' '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. * Helper function to wrap repetitive simple method templating.
* *
@ -67,7 +73,6 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
$options = $this->_parseOptions($options, $callbacks); $options = $this->_parseOptions($options, $callbacks);
return sprintf($template, $this->selection, $options); return sprintf($template, $this->selection, $options);
} }
/** /**
* Create javascript selector for a CSS rule * Create javascript selector for a CSS rule
* *
@ -76,13 +81,12 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
**/ **/
function get($selector) { function get($selector) {
if ($selector == 'window' || $selector == 'document') { if ($selector == 'window' || $selector == 'document') {
$this->selection = '$(' . $selector .')'; $this->selection = $this->jQueryObject . '(' . $selector .')';
} else { } else {
$this->selection = '$("' . $selector . '")'; $this->selection = $this->jQueryObject . '("' . $selector . '")';
} }
return $this; return $this;
} }
/** /**
* Add an event to the script cache. Operates on the currently selected elements. * Add an event to the script cache. Operates on the currently selected elements.
* *
@ -109,7 +113,6 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
} }
return sprintf('%s.bind("%s", %s);', $this->selection, $type, $callback); return sprintf('%s.bind("%s", %s);', $this->selection, $type, $callback);
} }
/** /**
* Create a domReady event. This is a special event in many libraries * Create a domReady event. This is a special event in many libraries
* *
@ -119,7 +122,6 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
function domReady($functionBody) { function domReady($functionBody) {
return $this->get('document')->event('ready', $functionBody, array('stop' => false)); return $this->get('document')->event('ready', $functionBody, array('stop' => false));
} }
/** /**
* Create an iteration over the current selection result. * Create an iteration over the current selection result.
* *
@ -130,7 +132,6 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
function each($callback) { function each($callback) {
return $this->selection . '.each(function () {' . $callback . '});'; return $this->selection . '.each(function () {' . $callback . '});';
} }
/** /**
* Trigger an Effect. * Trigger an Effect.
* *
@ -158,7 +159,6 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
} }
return $this->selection . $effect; return $this->selection . $effect;
} }
/** /**
* Create an $.ajax() call. * Create an $.ajax() call.
* *
@ -183,7 +183,6 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
$options = $this->_parseOptions($options, $callbacks); $options = $this->_parseOptions($options, $callbacks);
return '$.ajax({' . $options .'});'; return '$.ajax({' . $options .'});';
} }
/** /**
* Create a sortable element. * Create a sortable element.
* *
@ -199,7 +198,6 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
$template = '%s.sortable({%s});'; $template = '%s.sortable({%s});';
return $this->_methodTemplate('sortable', $template, $options, $callbacks); return $this->_methodTemplate('sortable', $template, $options, $callbacks);
} }
/** /**
* Create a Draggable element * Create a Draggable element
* *
@ -214,7 +212,6 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
$template = '%s.draggable({%s});'; $template = '%s.draggable({%s});';
return $this->_methodTemplate('drag', $template, $options, $callbacks); return $this->_methodTemplate('drag', $template, $options, $callbacks);
} }
/** /**
* Create a Droppable element * Create a Droppable element
* *
@ -229,7 +226,6 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
$template = '%s.droppable({%s});'; $template = '%s.droppable({%s});';
return $this->_methodTemplate('drop', $template, $options, $callbacks); return $this->_methodTemplate('drop', $template, $options, $callbacks);
} }
/** /**
* Create a Slider element * Create a Slider element
* *
@ -244,5 +240,21 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
$template = '%s.slider({%s});'; $template = '%s.slider({%s});';
return $this->_methodTemplate('slider', $template, $options, $callbacks); 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();';
}
} }
?> ?>

View file

@ -47,15 +47,15 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
$result = $this->Jquery->get('#content'); $result = $this->Jquery->get('#content');
$this->assertEqual($result, $this->Jquery); $this->assertEqual($result, $this->Jquery);
$this->assertEqual($this->Jquery->selection, '$("#content")'); $this->assertEqual($this->Jquery->selection, '$("#content")');
$result = $this->Jquery->get('document'); $result = $this->Jquery->get('document');
$this->assertEqual($result, $this->Jquery); $this->assertEqual($result, $this->Jquery);
$this->assertEqual($this->Jquery->selection, '$(document)'); $this->assertEqual($this->Jquery->selection, '$(document)');
$result = $this->Jquery->get('window'); $result = $this->Jquery->get('window');
$this->assertEqual($result, $this->Jquery); $this->assertEqual($result, $this->Jquery);
$this->assertEqual($this->Jquery->selection, '$(window)'); $this->assertEqual($this->Jquery->selection, '$(window)');
$result = $this->Jquery->get('ul'); $result = $this->Jquery->get('ul');
$this->assertEqual($result, $this->Jquery); $this->assertEqual($result, $this->Jquery);
$this->assertEqual($this->Jquery->selection, '$("ul")'); $this->assertEqual($this->Jquery->selection, '$("ul")');
@ -147,7 +147,7 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
$result = $this->Jquery->request('/people/edit/1', array( $result = $this->Jquery->request('/people/edit/1', array(
'method' => 'post', 'method' => 'post',
'before' => 'doBefore', 'before' => 'doBefore',
'complete' => 'doComplete', 'complete' => 'doComplete',
'success' => 'doSuccess', 'success' => 'doSuccess',
'error' => 'handleError', 'error' => 'handleError',
@ -156,7 +156,7 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
)); ));
$expected = '$.ajax({beforeSend:doBefore, complete:doComplete, data:"name=jim&height=185cm", dataType:"json", error:handleError, method:"post", success:doSuccess, url:"\\/people\\/edit\\/1"});'; $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); $this->assertEqual($result, $expected);
$result = $this->Jquery->request('/people/edit/1', array( $result = $this->Jquery->request('/people/edit/1', array(
'update' => '#updated', 'update' => '#updated',
'success' => 'doFoo', 'success' => 'doFoo',
@ -192,7 +192,7 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
$result = $this->Jquery->drag(array( $result = $this->Jquery->drag(array(
'container' => '#content', 'container' => '#content',
'start' => 'onStart', 'start' => 'onStart',
'drag' => 'onDrag', 'drag' => 'onDrag',
'stop' => 'onStop', 'stop' => 'onStop',
'snapGrid' => array(10, 10), 'snapGrid' => array(10, 10),
)); ));
@ -208,7 +208,7 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
$this->Jquery->get('#element'); $this->Jquery->get('#element');
$result = $this->Jquery->drop(array( $result = $this->Jquery->drop(array(
'accept' => '.items', 'accept' => '.items',
'hover' => 'onHover', 'hover' => 'onHover',
'leave' => 'onExit', 'leave' => 'onExit',
'drop' => 'onDrop' 'drop' => 'onDrop'
)); ));
@ -233,5 +233,24 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
$expected = '$("#element").slider({change:onChange, max:10, min:0, orientation:"vertical", stop:onComplete, value:2});'; $expected = '$("#element").slider({change:onChange, max:10, min:0, orientation:"vertical", stop:onComplete, value:2});';
$this->assertEqual($result, $expected); $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);
}
} }
?> ?>