Basically I want to fetch a JSON file and store it in a model. However, when I try to access the attributes via get() it returns undefined. So lets say the JSON file has an array games that consists of objects with some attributes. It doesn't really matter. Just want to save them in the model and access them. So I'm doing something like this.
var player = Backbone.Model.extend({
initialize: function(app, options) {
this.app = app;
var _this = this;
this.fetch({
url: "someurl",
success: function() {
console.log("success");
}
});
}
});
var instplayer = new player();
instplayer.on('change', function(){
console.log(model);
console.log(model.get(games));
})
So I figured that I need an event to ensure that get() is called when the data is really available. But this still returns undefined. What do I need to do?
{ username: "joe", games: [ { title:"Game One" }, { title: "Game Two" } ] }and then you just want to access thegamesproperty, is that right?.getmethod, likemodel.get("games")in quotes, I would also suggest to usesyncevent instead ofchange.syncwill make sure to fire when the content has come from the server.changewill fire on any change to that model data, which might be more often than you'd want.changewas fired before the data was fully loaded and that why I couldn't get() it. Thanks a lot.