When requesting for data.json
file for populating collection which has below data
[{
"Id": "BVwi1",
"Name": "Bag It",
"AverageRating": 4.6,
"ReleaseYear": 2010,
"Url": "http://www.netflix.com/Movie/Bag_It/70153545",
"Rating": "NR"
}, {
"Id": "BW1Ss",
"Name": "Lost Boy: The Next Chapter",
"AverageRating": 4.6,
"ReleaseYear": 2009,
"Url": "http://www.netflix.com/Movie/Lost_Boy_The_Next_Chapter/70171826",
"Rating": "NR"
}]
Collection does not invoke the "Reset" event as the documentation says it should. I can view the request and response are correct after the fetch method but nothing happens. Below is the code for my app. Router that start's everything
Theater.Router = Backbone.Router.extend({
routes: {
"": "defaultRoute"
},
defaultRoute: function () {
Theater.movies = new Theater.Collections.Movies()
new Theater.Views.Movies({
collection: Theater.movies
});
Theater.movies.fetch();
}
})
var appRouter = new Theater.Router();
Backbone.history.start();
the Collection
Theater.Collections.Movies = Backbone.Collection.extend({
model: Theater.Models.Movie,
url: "scripts/data/data.json",
initialize: function () {}
});
View that subscribes to the reset event
Theater.Views.Movies = Backbone.View.extend({
initialize: function () {
_.bindAll(this, "render", "addOne");
this.collection.bind("reset", this.render);
this.collection.bind("add", this.addOne);
},
render: function(){
console.log("render")
console.log(this.collection.length);
},
addOne: function (model) {
console.log("addOne")
}
})
http://bardevblog.wordpress.com/2012/01/16/understanding-backbone-js-simple-example/
What events are triggered when calling fetch () on a Backbone.js collection? - Stack Overflow good point. When I wish to refresh a view based on a model fetch (), I bind to 'sync'. That's what is triggered by default in 1.0. As of backbone 1.0, model.fetch () triggers a 'sync'. That's what you should bind to.
Backbone.js. Backbone.js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface. The project is hosted on GitHub , ...
The Backbone object itself mixes in Events, and can be used to emit any global events that your application needs. "add" (model, collection, options) — when a model is added to a collection. "remove" (model, collection, options) — when a model is removed from a collection.
Aside from their own events, collections also proxy through all of the events that occur to models within them, allowing you to listen in one place for any change that might happen to any model in the collection. Backbone is pre-configured to sync with a RESTful API.
You should tell Backbone to fire the reset on fetch by passing {reset: true} when fetching as of Backbone 1.0
Replace :
Theater.movies.fetch()
With
Theater.movies.fetch({reset :true})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With