Skip to content

Instantly share code, notes, and snippets.

@leehambley
Created September 6, 2011 13:00
Show Gist options
  • Save leehambley/1197469 to your computer and use it in GitHub Desktop.
Save leehambley/1197469 to your computer and use it in GitHub Desktop.
Backbone and Underscore
BestLovedProducts.Controllers.AffiliateProducts = Backbone.Router.extend({
routes: {
"": "index"
},
index: function() {
var affiliate_products = new BestLovedProducts.Collections.AffiliateProducts();
affiliate_products.fetch({
success: function() {
new BestLovedProducts.Views.Index({ collection: affiliate_products});
},
error: function() {
new Error({message: "Error loading Affiliate Products"});
}
});
affiliate_products.lazyFetch = _.debounce('reset', 300);
//var lazyFetch = _.debounce(affiliate_products.fetch(), 300);
//$("#search_value").bind("keyup", lazyFetch);
//
$("#search_value").bind("keyup", function() {
affiliate_products.lazyFetch();
});
}
});
BestLovedProducts.Views.Index = Backbone.View.extend({
initialize: function() {
_.bindAll(this, 'render');
this.collection.bind('reset', this.render);
this.render();
},
render: function() {
$(this.el).html(JST.affiliate_products_collection({ collection: this.collection }));
$('#affiliate-products').html(this.el);
}
});
BestLovedProducts.Collections.AffiliateProducts = Backbone.Collection.extend({
model: AffiliateProduct,
url: function() {
return "/admin/affiliate_products?search_value=" + $("#search_value").val();
}
});
var AffiliateProduct = Backbone.Model.extend({
url : function() {
var base = 'affiliate_products';
return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + this.id;
}
});
var BestLovedProducts = {
Views: {},
Controllers: {},
Collections: {},
init: function() {
new BestLovedProducts.Controllers.AffiliateProducts();
Backbone.history.start();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment