I have a Backbone model:
var User = Backbone.Model.extend({
idAttribute: '_id',
url: '/api/user',
defaults:
{ username: ''
},
initialize: function () {
console.log(this, this.isNew());
}
});
Then I initialise the model and fetch the instance:
var user = new User();
user.fetch();
This works. If I inspect user, there is an id property. It exists on the server. However, for some reason user.isNew() is reporting true – even though it has an id property set (property as well as attribute). I need to update this model on the server but Backbone insists on sending POST requests, when I need PUT…
initialize runs before the fetch is completed. isNew value will change until the server answer arrives. Listen for the change event on the model or pass a callback to the fetch call.