Created
September 6, 2011 13:00
-
-
Save leehambley/1197469 to your computer and use it in GitHub Desktop.
Backbone and Underscore
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
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