By Date: <-- -->
By Thread: <-- -->

[PATCH] Allow generic autocompleter (Ajax.Watcher)



Um no, in-house project, but it's not difficult.

new Ajax.Watcher(element, url, callback, options);

Callback receives the ajax response. It gets passed to onSuccess in Ajax.Request. One interesting options - firstRun (bool), set whether the ajax request + callback should be performed on instantiation or not. Default not.

-Rob

Deco Rior wrote:
Do have an example for this
On Feb 13, 2006, at 10:10 AM, Robin Haswell wrote:

Hey. Below is a patch to allow generic Ajax.Autocompleters. Basically
it's for people who wanna be able to watch an input for changes, but
don't want it to pop up an autocompleter box below. Useful for live
previews, that kind of thing.

Someone's gonna have to fix the tabstops. I couldn't be arse to work out
how to make vim do the softtabs properly. Spaces for indentation is
stupid anyway.

-Rob

--- js/controls.js.old 2006-02-13 16:48:19.000000000 +0000
+++ js/controls.js 2006-02-13 14:48:28.000000000 +0000
(at) (at) -336,6 +336,45 (at) (at)


 });

+Ajax.Watcher = Class.create();
+Ajax.Watcher.prototype = {
+       initialize: function(element, url, callback, options) {
+               this.options = Object.extend({
+                       waitTime: 3,
+                       frequency: 0.4
+               }, options);
+               this.element = $(element);
+               this.url = url;
+               this.callback = callback;
+               this.observer = null;
+               this.active = false;
+               this.changed = false;
+               Event.observe(this.element, "keypress",
this.onKeyPress.bindAsEventListener(this));
+               if (this.options.firstRun) {
+                       this.onObserverEvent();
+               }
+       },
+  onKeyPress: function(event) {
+    this.changed = true;
+    this.hasFocus = true;
+
+    if(this.observer) clearTimeout(this.observer);
+      this.observer =
+        setTimeout(this.onObserverEvent.bind(this),
this.options.frequency*1000);
+  },
+  onObserverEvent: function() {
+       this.changed = false;
+       data = escape(this.element.value);
+               new Ajax.Request(this.url, Object.extend(this.options, {
+                       onSuccess:this.callback,
+                       method:"post",
+                       postBody:"value="+data}));
+  },
+
+};
+
+
+
 // The local array autocompleter. Used when you'd prefer to
 // inject an array of autocompletion options into the page, rather
 // than sending out Ajax queries, which can be quite slow sometimes.
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs (at) lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs

_______________________________________________ Rails-spinoffs mailing list Rails-spinoffs (at) lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs (at) lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs