(function($) {
Product = Backbone.Model.extend({
// Create a model to hold product atribute
name: null
});
Products = Backbone.Collection.extend({
//This is our Products collection and holds our Product models
initialize: function(models, options) {
this.bind('add', options.view.addProductLi);
this.bind('show', options.view.showProducts);
//Listen for new additions to the collection and call a view function if so
}
});
ProductView = Backbone.View.extend({
el: $('body'),
initialize: function() {
// create a products collection when the view is initialized
//passing it a reference to this view to create a connection between the two
this.products = new Products(null, {
view: this
});
this.updateCount();
},
events: {
'click #add-product': 'addProducts',
'click #show-products': 'showProducts'
},
addProducts: function() {
var product_name = prompt('What is the product?');
var product_model = new Product({
name: product_name
});
//Add a new product model to our product collection
this.products.add(product_model);
this.updateCount();
},
showProducts: function() {
if ($('#product-list').is(':hidden')) {
$('#product-list').show();
$('#show-products').text('Hide Products');
} else {
$('#product-list').hide();
$('#show-products').text('Show Products');
}
},
updateCount: function() {
var count = Number($('#product-list li').length);
$('#total_product_count').text(count);
},
addProductLi: function(model) {
//The parameter passed is a reference to the model...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.