Skip to content

Instantly share code, notes, and snippets.

@easierbycode
Forked from mxriverlynn/manualcall.js
Created December 22, 2011 17:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save easierbycode/1511144 to your computer and use it in GitHub Desktop.
Save easierbycode/1511144 to your computer and use it in GitHub Desktop.
search with backbone collections
var results = new SearchResults();
results.searchTerm = "some search term";
results.fetch({
success: someView.showTheResults
});
// handle the search results in a view
Backbone.View.extend({
initialize: function(){
MyApp.vent.bind("search:results", this.showResults, this);
},
showResults: function(results){
this.collection = results;
this.render();
},
render: function(){
var html = $("#some-template").tmpl(this.collection.toJSON());
$(this.el).html(html);
}
});
// do the actual search, based on a search
// term that was entered in to the search box
SearchResults.search("some search term");
SearchResults = Backbone.Collection.extend({
url: function(){
return "/items/" + this.searchTerm;
}
}, {
search: function(searchTerm){
var results = new SearchResults();
results.searchTerm = "some search term";
results.fetch({
success: function(){
MyApp.vent.trigger("search:results", results);
},
error: function(collection, response){
MyApp.vent.trigger("search:error", response);
}
});
}
});
SearchResults = Backbone.Collection.extend({
url: function(){
return "/items/" + this.searchTerm;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment