Skip to content

Instantly share code, notes, and snippets.

@phiggins42
Created September 5, 2009 20:37
Show Gist options
  • Save phiggins42/181503 to your computer and use it in GitHub Desktop.
Save phiggins42/181503 to your computer and use it in GitHub Desktop.
dojo.declare('hitpost.Search', dijit._Widget, {
// not necessary:
// searchTimeout : null,
cache : {}, // NOTE: thisis a shared member, like a static. create it in postCreate
// if you want a unique cache for each instance. Otherwise, thisis fine.
// no need for a blank constructor
// constructor : function(args) { },
postCreate : function() {
this.target = dojo.byId('search_results');
this.input = dojo.query('#search input')[0];
// use _Widget's .connect method. it scopes and disconnects for you
this.connect(this.input, "onkeyup", "_doSearch");
this.connect(this.input, "onblur", "_hideSearch");
},
_hideSearch : function() {
dojo.removeClass(this.target, 'visible');
},
_doSearch : function() {
this.searchTimeout && clearTimeout(this.searchTimeout);
this.searchTimeout = setTimeout(dojo.hitch(this, "_realSearch"), 300);
},
_realSearch: function() {
var val = this.input.value;
dojo.addClass(this.input, 'loading');
if (val.length > 2) {
if (this.cache[val]) {
this._handleResult(this.cache[val]);
} else {
var xhr = dojo.xhrGet({
url : '/html/search/' + val,
// handleAs : 'text', // text is default
load : dojo.hitch(this, "_handleResult")
});
}
} else {
this._hideSearch();
}
},
_handleResult : function(html) {
dojo.removeClass(this.input, 'loading');
dojo.addClass(this.target, 'visible');
this.target.innerHTML = html;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment