Skip to content

Instantly share code, notes, and snippets.

@keithbloom
Created October 30, 2011 14:41
Show Gist options
  • Save keithbloom/1325973 to your computer and use it in GitHub Desktop.
Save keithbloom/1325973 to your computer and use it in GitHub Desktop.
WonderfulBackboneJS
$(function() {
// 1
window.ShoppingItem = Backbone.Model.extend({
constructor: function () {
if (!this.collection) {
this.collection = Products;
}
},
defaults: {
title: "Empty item..",
state: "To buy"
}
});
// 2
window.ShoppingList = Backbone.Collection.extend({
url: 'list/',
model: ShoppingItem
});
// 3
window.Products = new ShoppingList();
});
var item = new window.ShoppingItem;
item.save(title: ‘Eggs’);
eggs.set(state: ‘Bought’);
eggs.destroy;
window.ShoppingList = Backbone.Collection.extend({
url: 'list/',
model: Item,
constructor: function ShoppingList() {
Backbone.Collection.prototype.constructor.apply(this, arguments);
},
toobuy: function () {
return this.filter(function (item) { return item.get('state') === 'To buy'; });
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment