I am the author of the Rails plugin model_auto_completer, and would
like to ask you for advice on how to handle a particular feature.
The idea of the plugin is that the server sends the model ID somehow
embedded in the ID attribute of each completion LIs. The user selects
some LI as usual, but the plugin hooks into afterUpdateElement to
extract the associated ID and store it in a hidden field behind the
scenes. Fine.
The goal of that widget is to auto complete _models_ so care is needed
with free text. The texfield needs to be editable so that the user can
enter text to auto complete, but if the user leaves something that
didn''t come from some autocompletion the widget does something to tell
the user that value is not allowed, or else sets the hidden ID to the
empty string, depending on a flag.
To implement that the widget maintains a private cache in the text
field that stores the last selection. This is done among other stuff
in afterUpdateElement:
function(element, value) {
/* ... */
element.model_auto_completer_cache = element.value;
/* ... */
}
Now, when the user is "done" editing, we check the cache and decide
what to do (I hope you don''t mind a little Ruby):
tag_options[:onblur] = if options[:allow_free_text]
"if (this.value != this.model_auto_completer_cache) {$
(''#{hf_id}'').value = ''''}"
else
''this.value = this.model_auto_completer_cache''
end
This works fine if the user uses the keyboard, but if the user uses
the mouse the click on the LI triggers onblur, and there''s a race
condition. Well I am not sure whether there''s a race condition or
there''s a guaranteed order of handler execution, but what I know for
sure is that they interfere with each other.
I''ve thought I could put some little sleep before we check the cache.
But if a selection was already done, free text is allowed, the return
key sends the form, and the user writes some free text and hits
return, I guess the textfield might not be able to set the hidden
field to the empty string.
Can you think of any robust approach to handle this?
-- fxn
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---