Created
October 19, 2011 14:32
-
-
Save meirish/1298469 to your computer and use it in GitHub Desktop.
Lstn Search View
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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