function test_remote_function() { $foo = PrototypeHelper::remote_function(array('update' => 'options', 'url' => 'http://update')); $exp = <<<EXPECTED new Ajax.Updater('options', 'http://update', {asynchronous:true, evalScripts:false}) EXPECTED; $this->assertEqual($exp, $foo); }
/** * @ignore */ function build_observer($klass, $name, $options = array()) { if (!isset($options['with']) && $options['update']) { $options['with'] = 'value'; } $callback = PrototypeHelper::remote_function($options); $javascript = 'new ' . $klass . '("' . $name . '", '; if (isset($options['frequency'])) { $javascript .= $options['frequency'] . ", "; } $javascript .= 'function(element, value) {'; $javascript .= $callback . '});'; return JsHelper::javascript_tag($javascript); }
<li id="item_<?php echo $item->id; ?> "> <input type="checkbox" name="item[<?php echo $item->id; ?> ][done]" <?php echo $item->done ? 'checked="checked"' : ''; ?> onClick="<?php echo PrototypeHelper::remote_function(array('url' => $controller->url_for('todo/toggle/' . $item->id))); ?> " /> <?php echo $item->description; ?> <a href="<?php echo $controller->url_for('todo/edit/' . $item->id); ?> ">edit</a> <a href="<?php echo $controller->url_for('todo/delete/' . $item->id); ?> ">delete</a> </li>
/** * Makes the elements with the DOM ID specified by '$element_id' sortable * by drag-and-drop and make an AJAX call whenever the sort order has * changed. By default, the action called gets the serialized sortable * element as parameters. * * Example: * <php echo sortable_element($my_list, array( * 'url' => '@order', * )) ?> * * In the example, the action gets a '$my_list' array parameter * containing the values of the ids of elements the sortable consists * of, in the current order. * * You can change the behaviour with various options, see * http://script.aculo.us for more documentation. */ function sortable_element($element_id, $options = array()) { if (!isset($options['with'])) { $options['with'] = "Sortable.serialize('{$element_id}')"; } if (!isset($options['onUpdate'])) { $options['onUpdate'] = sprintf('function(){%s}', PrototypeHelper::remote_function($options)); } foreach (PrototypeHelper::get_ajax_options() as $key) { unset($options[$key]); } foreach (array('tag', 'overlap', 'constraint', 'handle') as $option) { if (isset($options[$option])) { $options[$option] = "'{$options[$option]}'"; } } if (isset($options['containment'])) { $options['containment'] = self::array_or_string_for_javascript($options['containment']); } if (isset($options['hoverclass'])) { $options['hoverclass'] = "'{$options['hoverclass']}'"; } if (isset($options['only'])) { $options['only'] = self::array_or_string_for_javascript($options['only']); } return JsHelper::javascript_tag(sprintf("Sortable.create('%s', %s)", $element_id, JsHelper::options_for_javascript($options))); }
/** * Makes the element with the DOM ID specified by +element_id+ receive * dropped draggable elements (created by draggable_element). * and make an AJAX call By default, the action called gets the DOM ID * of the element as parameter. * * Example: * <%= drop_receiving_element("my_cart", :url => * { :controller => "cart", :action => "add" }) %> * * You can change the behaviour with various options, see * http://script.aculo.us for more documentation. */ function drop_receiving_element($element_id, $options = array()) { $options['with'] = !empty($options['with']) ? $options['with'] : "'id='" . urlencode(element . id); $options['onDrop'] = !empty($options['onDrop']) ? $options['onDrop'] : "function(element){" . PrototypeHelper::remote_function($options) . "}"; foreach ($options as $key => $option) { /** * @todo: fix this code when implemented PrototypeHelper * * if (in_array(PrototypeHelper::AJAX_OPTIONS[$option])) { * unset($options[$option]); * } */ } if (in_array($options['accept'])) { $options['accept'] = JavascriptHelper::_array_or_string_for_javascript($options['accept']); } if (in_array($options['hoverclass'])) { $options['hoverclass'] = "'{$options['hoverclass']}'"; } return JavascriptHelper::javascript_tag("Droppables.add('{$element_id}', " . JavascriptHelper::_options_for_javascript($options) . ")"); }