Created
October 30, 2011 14:41
-
-
Save keithbloom/1325973 to your computer and use it in GitHub Desktop.
WonderfulBackboneJS
This file contains hidden or 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
$(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(); | |
}); |
This file contains hidden or 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
var item = new window.ShoppingItem; | |
item.save(title: ‘Eggs’); | |
eggs.set(state: ‘Bought’); | |
eggs.destroy; |
This file contains hidden or 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
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