Skip to content

Instantly share code, notes, and snippets.

@meirish
Created October 19, 2011 14:32
Show Gist options
  • Save meirish/1298469 to your computer and use it in GitHub Desktop.
Save meirish/1298469 to your computer and use it in GitHub Desktop.
Lstn Search View
Lstn.Views.SearchView = Backbone.View.extend({
el: '#search',
events:{
'focus':'bindToReset',
'blur':'unbindReset'
},
initialize: function(options){
_.bindAll(this, 'suggestSearch');
var throttleSugg = _.throttle( this.suggestSearch, 700 );
$(this.el).keypress( throttleSugg );
},
suggestSearch: function(e){
var self = this,
query = $(this.el).val();
if (query.length > 2){
Lstn.searchresult.fetch({data: {q:query}});
}
if(query.length === 0 && this.results){
this.results.trigger('hide');
}
},
refreshResults: function(){
if (this.results){
this.results.trigger('reset');
} else {
this.results = new Lstn.Views.SearchSuggestView;
this.results.trigger('render');
}
},
bindToReset: function(){
Lstn.searchresult.bind('reset', this.refreshResults, this);
},
unbindReset: function(){
Lstn.searchresult.unbind('reset');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment