[jQuery] Unobtrusively passing data
I'm building a rails app and I've been trying to keep my html as
unobtrusive as possible and my application.js as general as possible.
Up until now I've been able to build re-usable page components by
creating specific layout structure with the occasional custom
attribute. Now I'm trying to integrate an autocomplete into my pages
and am looking for a clean way to get data to the autocomplete
component.
Here's what I do...
In my view html
I create the form element for autocomplete with
class='autocompleter'
With javascript I add the data source to a global array that is
indexed by element id. This lets me pass arrays etc.
In my application.js
I added the following function called from my ready function.
var autocomplete_data = [];
function load_autocompleters() {
$(".autocompleted").each(function() {
$(this).autocomplete(autocomplete_data[this.id], {
matchContains: true,
minChars: 0
});
});
}
This works just fine, but is there a better way to do this?