Ajax.Autocompleter and events after the list of choices is updated

8 views
Skip to first unread message

RidderGraniet

unread,
Jul 6, 2009, 6:16:09 AM7/6/09
to Prototype & script.aculo.us
I discovered that with the Autocompleter function in scriptaculous
doesn't provide a satisfying 'afterUpdateChoices' callback. This, in
my opinion, could be very usefull if you want to do something with the
list items that are populated in the div element you provide the
autocompleter with, for instance add mouse observers for custom
behavior on every list item. Because you need to know when the list is
updated and you can access the elements inside it, I figured I needed
a callback just after the list is populated.

Since onComplete ajax callback doesn't seem to work for me, I decided
to tweak the code a little bit so you can add a callback for
'afterUpdateChoices'. It are 2 simple lines, really:

updateChoices: function(choices) {
if(!this.changed && this.hasFocus) {
this.update.innerHTML = choices;
Element.cleanWhitespace(this.update);
Element.cleanWhitespace(this.update.down());

if(this.update.firstChild && this.update.down().childNodes) {
this.entryCount =
this.update.down().childNodes.length;
for (var i = 0; i < this.entryCount; i++) {
var entry = this.getEntry(i);
entry.autocompleteIndex = i;
this.addObservers(entry);
}
} else {
this.entryCount = 0;
}

this.stopIndicator();
this.index = 0;

if(this.entryCount==1 && this.options.autoSelect) {
this.selectEntry();
this.hide();
} else {
this.render();
}
}

if(this.options.afterUpdateChoices)
this.options.afterUpdateChoices(choices);
},

It are the lower two lines you need to add to the updateChoices
function, and of course the option in your Autocompleter init with a
valid function.

Unless someone comes with a better way to get this kind of control, I
suggest it to be added in a next release :).

Richard Quadling

unread,
Jul 6, 2009, 9:41:51 AM7/6/09
to prototype-s...@googlegroups.com
2009/7/6 RidderGraniet <co.d...@gmail.com>:
Adding observers to elements which may disappear (say I don't like the
results of the autocomplete and delete it and enter something else) is
bad.

The observer should be on a static container for the elements and
allow the event to bubble up. Much safer. That way you can be as
dynamic to the content as you want and you don't have to track which
dynamic elements have observers.



--
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"
I need a car : http://snipurl.com/l4pih
ZOPA : http://uk.zopa.com/member/RQuadling
Reply all
Reply to author
Forward
0 new messages