Пример #1
0
 /**
  * Executed after a view has rendered, used to include bufferred code
  * blocks.
  *
  * @access public
  */
 function afterRender()
 {
     if (env('HTTP_X_UPDATE') != null && !empty($this->__ajaxBuffer)) {
         @ob_end_clean();
         $data = array();
         $divs = explode(' ', env('HTTP_X_UPDATE'));
         $keys = array_keys($this->__ajaxBuffer);
         if (count($divs) == 1 && in_array($divs[0], $keys)) {
             echo $this->__ajaxBuffer[$divs[0]];
         } else {
             foreach ($this->__ajaxBuffer as $key => $val) {
                 if (in_array($key, $divs)) {
                     $data[] = $key . ':"' . rawurlencode($val) . '"';
                 }
             }
             $out = 'var __ajaxUpdater__ = {' . implode(", \n", $data) . '};' . "\n";
             $out .= 'for (n in __ajaxUpdater__) { if (typeof __ajaxUpdater__[n] == "string"';
             $out .= ' && $(n)) Element.update($(n), unescape(decodeURIComponent(';
             $out .= '__ajaxUpdater__[n]))); }';
             echo $this->Javascript->codeBlock($out, false);
         }
         $scripts = $this->Javascript->getCache();
         if (!empty($scripts)) {
             echo $this->Javascript->codeBlock($scripts, false);
         }
         $this->_stop();
     }
 }
Пример #2
0
 /**
  * Create a text field with Jquery UI Autocomplete.
  *
  * Creates an autocomplete field with the given ID and options.
  * needs include jQuery UI Autocomplete file
  *
  * @param string $field DOM ID of field to observe
  * @param array $options Ajax options
  * @return string Ajax script
  * check out http://jqueryui.com/demos/autocomplete/
  */
 function autoComplete($field, $options = array())
 {
     $var = '';
     if (isset($options['var'])) {
         $var = 'var ' . $options['var'] . ' = ';
         unset($options['var']);
     }
     if (isset($options['source'])) {
         $options['source'] = "'" . Router::url($options['source']) . "'";
     }
     if (!isset($options['id'])) {
         $options['id'] = Inflector::camelize(str_replace(".", "_", $field));
     }
     $htmlOptions = $this->__getHtmlOptions($options);
     $htmlOptions['autocomplete'] = "off";
     foreach ($this->autoCompleteOptions as $opt) {
         unset($htmlOptions[$opt]);
     }
     $options = $this->_optionsToString($options, array('multipleSeparator'));
     $callbacks = array('formatItem', 'formatMatch', 'formatResult', 'highlight');
     foreach ($callbacks as $callback) {
         if (isset($options[$callback])) {
             $name = $callback;
             $code = $options[$callback];
             switch ($name) {
                 case 'formatResult':
                     $options[$name] = "function(data, i, max) {" . $code . "}";
                     break;
                 case 'highlight':
                     $options[$name] = "function(data, search) {" . $code . "}";
                     break;
                 default:
                     $options[$name] = "function(row, i, max, term) {" . $code . "}";
                     break;
             }
         }
     }
     $options = $this->_buildOptions($options, $this->autoCompleteOptions);
     $text = $this->Form->text($field, $htmlOptions);
     $script = "{$var} \$('#{$htmlOptions['id']}').autocomplete(";
     $script .= "{$options});";
     return "{$text}\n" . $this->Javascript->codeBlock($script);
 }