From b28bb50520c54daec5d1fde1b807a50917ea29bf Mon Sep 17 00:00:00 2001
From: Loren Sands-Ramshaw
-var ships = new Backbone.Collection;
+var Ship = Backbone.Model.extend({
+ defaults: {
+ "name": "Black Pearl"
+ }
+});
-ships.bind("add", function(ship) {
+var Fleet = Backbone.Collection.extend({
+ model: Ship
+});
+
+var pirates = new Fleet();
+
+pirates.bind("add", function(ship) {
alert("Ahoy " + ship.get("name") + "!");
});
-ships.add([
+pirates.add([
{name: "Flying Dutchman"},
- {name: "Black Pearl"}
+ {captain: "Jack Sparrow"}
]);
@@ -1585,7 +1595,7 @@ $(function(){
- Backbone.sync is the function the Backbone calls every time it
+ Backbone.sync is the function that Backbone calls every time it
attempts to read or save a model to the server. By default, it uses
(jQuery/Zepto).ajax to make a RESTful JSON request. You can override
it in order to use a different persistence strategy, such as WebSockets,
From 245acb08213c5cbdac8c6f860cdf6883c9720fc8 Mon Sep 17 00:00:00 2001
From: Loren Sands-Ramshaw
- In the following example, notice how because the model has never been - saved previously, our overridden version of Backbone.sync receives a "create" request. + In the following example, notice how our overridden version + of Backbone.sync receives a "create" request + the first time the model is saved and an "update" + request the second time.
Backbone.sync = function(method, model) {
alert(method + ": " + JSON.stringify(model));
+ model.id = 1;
};
var book = new Backbone.Model({
@@ -773,6 +776,10 @@ var book = new Backbone.Model({
});
book.save();
+
+book.save({
+ author: "Teddy"
+});