I’m having trouble with this in a backbone.js project.
This is my view:
app.SomeView = Backbone.View.extend({
render: function() {
var that = this;
$.getJSON(someURL, function(result) {
that.property = result.something;
});
return this;
}
})
Inexplicably, inside the getJSON callback that.property is set, but as soon as that function is finished – i.e. at return this – that.property equals undefined, same as this.property.
What am I doing wrong?
Not sure why you are not using Models. Answering to your question, there are different solutions, the first one:
Using events:
Second one, you need to add a callback and pass it:
My answers was written for fix the question that you created. But for a long term improvement, do you know that Models has a fetch method that basically loads JSON from the server and associate it the Model?
http://backbonejs.org/#Model-fetch
This is how I will load the JSON: