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'
)
);
/**
* 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 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
$options = $this->_parseOptions($options, $callbacks);
return sprintf($template, $this->selection, $options);
}
/**
* Create javascript selector for a CSS rule
*
@ -76,13 +81,12 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
**/
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 @@ 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
*
@ -119,7 +122,6 @@ 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.
*
@ -130,7 +132,6 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
function each($callback) {
return $this->selection . '.each(function () {' . $callback . '});';
}
/**
* Trigger an Effect.
*
@ -158,7 +159,6 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
}
return $this->selection . $effect;
}
/**
* Create an $.ajax() call.
*
@ -183,7 +183,6 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
$options = $this->_parseOptions($options, $callbacks);
return '$.ajax({' . $options .'});';
}
/**
* Create a sortable element.
*
@ -199,7 +198,6 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
$template = '%s.sortable({%s});';
return $this->_methodTemplate('sortable', $template, $options, $callbacks);
}
/**
* Create a Draggable element
*
@ -214,7 +212,6 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
$template = '%s.draggable({%s});';
return $this->_methodTemplate('drag', $template, $options, $callbacks);
}
/**
* Create a Droppable element
*
@ -229,7 +226,6 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
$template = '%s.droppable({%s});';
return $this->_methodTemplate('drop', $template, $options, $callbacks);
}
/**
* Create a Slider element
*
@ -244,5 +240,21 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
$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();';
}
}
?>

View file

@ -233,5 +233,24 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
$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);
}
}
?>