mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 10:36:16 +00:00
Implementing serializeForm() in jquery engine.
This commit is contained in:
parent
76c13fa98e
commit
36722c0ef2
2 changed files with 51 additions and 20 deletions
|
@ -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();';
|
||||
}
|
||||
}
|
||||
?>
|
|
@ -47,15 +47,15 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
|
|||
$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 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
|
|||
|
||||
$result = $this->Jquery->request('/people/edit/1', array(
|
||||
'method' => 'post',
|
||||
'before' => 'doBefore',
|
||||
'before' => 'doBefore',
|
||||
'complete' => 'doComplete',
|
||||
'success' => 'doSuccess',
|
||||
'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"});';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
|
||||
$result = $this->Jquery->request('/people/edit/1', array(
|
||||
'update' => '#updated',
|
||||
'success' => 'doFoo',
|
||||
|
@ -192,7 +192,7 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
|
|||
$result = $this->Jquery->drag(array(
|
||||
'container' => '#content',
|
||||
'start' => 'onStart',
|
||||
'drag' => 'onDrag',
|
||||
'drag' => 'onDrag',
|
||||
'stop' => 'onStop',
|
||||
'snapGrid' => array(10, 10),
|
||||
));
|
||||
|
@ -208,7 +208,7 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
|
|||
$this->Jquery->get('#element');
|
||||
$result = $this->Jquery->drop(array(
|
||||
'accept' => '.items',
|
||||
'hover' => 'onHover',
|
||||
'hover' => 'onHover',
|
||||
'leave' => 'onExit',
|
||||
'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});';
|
||||
$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);
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
Add table
Reference in a new issue