Permalink
Browse files

Adding additional tests to Request and updating callback argument list.

  • Loading branch information...
markstory committed Jul 28, 2009
1 parent 2e82d22 commit 75026715d153d01742dc37552a6500bf8e30cb7f
@@ -364,6 +364,13 @@ class JsBaseEngineHelper extends AppHelper {
**/
var $bufferedMethods = array('event', 'sortable', 'drag', 'drop', 'slider');
+/**
+ * Contains a list of callback names -> default arguments.
+ *
+ * @var array
+ **/
+ var $_callbackArguments = array();
+
/**
* Constructor.
*
@@ -872,8 +879,14 @@ function serializeForm() {
**/
function _parseOptions($options, $safeKeys = array()) {
$out = array();
+ $safeKeys = array_flip($safeKeys);
+ $wrapCallbacks = true;
+ if (isset($options['wrapCallbacks'])) {
+ $wrapCallbacks = $options['wrapCallbacks'];
+ unset($options['wrapCallbacks']);
+ }
foreach ($options as $key => $value) {
- if (!is_int($value) && !in_array($key, $safeKeys)) {
+ if (!is_int($value) && !isset($safeKeys[$key])) {
$value = $this->value($value);
}
$out[] = $key . ':' . $value;
@@ -904,6 +917,42 @@ function _mapOptions($method, $options) {
return $options;
}
+/**
+ * Prepare callbacks and wrap them with function ([args]) { } as defined in
+ * _callbackArgs array.
+ *
+ * @param string $method Name of the method you are preparing callbacks for.
+ * @param array $options Array of options being parsed
+ * @param string $callbacks Keys that contain callbacks
+ * @access protected
+ * @return array Array of options with callbacks added.
+ **/
+ function _prepareCallbacks($options, $callbacks = array()) {
+ $wrapCallbacks = true;
+ if (isset($options['wrapCallbacks'])) {
+ $wrapCallbacks = $options['wrapCallbacks'];
+ }
+ unset($options['wrapCallbacks']);
+ if (!$wrapCallbacks) {
+ return $options;
+ }
+
+ foreach ($callbacks as $callback) {
+ if (!isset($options[$callback])) {
+ continue;
+ }
+ $args = null;
+ if (isset($this->_callbackArguments[$callback])) {
+ $args = $this->_callbackArguments[$callback];
+ }
+ if (empty($args)) {
+ continue;
+ }
+ $options[$callback] = 'function (' . $args . ') {' . $options[$callback] . '}';
+ }
+ return $options;
+ }
+
/**
* Convert an array of data into a query string
*
@@ -69,6 +69,29 @@ class PrototypeEngineHelper extends JsBaseEngineHelper {
'value' => 'sliderValue',
)
);
+
+/**
+ * Contains a list of callback names -> default arguments.
+ *
+ * @var array
+ **/
+ var $_callbackArguments = array(
+ 'onSlide' => 'event',
+ 'onChange' => 'event',
+ 'onHover' => 'event',
+ 'onDrop' => 'event',
+ 'onStart' => 'event',
+ 'change' => 'event',
+ 'onDrag' => 'event',
+ 'onUpdate' => 'event',
+ 'onEnd' => 'event',
+ 'onCreate' => 'transport',
+ 'onComplete' => 'transport',
+ 'onFailure' => 'response, jsonHeader',
+ 'onRequest' => 'transport',
+ 'onSuccess' => 'response, jsonHeader'
+ );
+
/**
* Create javascript selector for a CSS rule
*
@@ -97,7 +120,7 @@ function get($selector) {
* - 'wrap' - Whether you want the callback wrapped in an anonymous function. (defaults true)
* - 'stop' - Whether you want the event to stopped. (defaults true)
*
- * @param string $type Type of event to bind to the current dom id
+ * @param string $type Type of event to bind to the current 946 id
* @param string $callback The Javascript function you wish to trigger or the function literal
* @param array $options Options for the event.
* @return string completed event handler
@@ -201,6 +224,7 @@ function request($url, $options = array()) {
unset($options['update'], $options['type']);
}
$callbacks = array('onCreate', 'onComplete', 'onFailure', 'onRequest', 'onSuccess');
+ $options = $this->_prepareCallbacks($options, $callbacks);
if (isset($options['dataExpression'])) {
$callbacks[] = 'parameters';
unset($options['dataExpression']);
@@ -223,6 +247,7 @@ function request($url, $options = array()) {
function sortable($options = array()) {
$options = $this->_mapOptions('sortable', $options);
$callbacks = array('onStart', 'change', 'onDrag', 'onDrop', 'onChange', 'onUpdate', 'onEnd');
+ $options = $this->_prepareCallbacks($options, $callbacks);
$options = $this->_parseOptions($options, $callbacks);
if (!empty($options)) {
$options = ', {' . $options . '}';
@@ -241,6 +266,7 @@ function sortable($options = array()) {
function drag($options = array()) {
$options = $this->_mapOptions('drag', $options);
$callbacks = array('onStart', 'change', 'onDrag', 'onEnd');
+ $options = $this->_prepareCallbacks($options, $callbacks);
$options = $this->_parseOptions($options, $callbacks);
if (!empty($options)) {
$options = ', {' . $options . '}';
@@ -262,6 +288,7 @@ function drag($options = array()) {
function drop($options = array()) {
$options = $this->_mapOptions('drop', $options);
$callbacks = array('onHover', 'onDrop');
+ $options = $this->_prepareCallbacks($options, $callbacks);
$options = $this->_parseOptions($options, $callbacks);
if (!empty($options)) {
$options = ', {' . $options . '}';
@@ -284,6 +311,8 @@ function slider($options = array()) {
$callbacks = array('onSlide', 'onChange');
$options = $this->_mapOptions('slider', $options);
+ $options = $this->_prepareCallbacks($options, $callbacks);
+
if (isset($options['min']) && isset($options['max'])) {
$options['range'] = array($options['min'], $options['max']);
unset($options['min'], $options['max']);
Oops, something went wrong.

0 comments on commit 7502671

Please sign in to comment.