user606521
user606521

Reputation: 15474

model's fetch does not work

Here is body of my model

urlRoot: '/users',

parse: function(response) {
   return response.User;
}

Here is what is returned when I type /users/1 in my browser:

{"User":{"id":"1","created":"2013-02-13 09:22:42","modified":"2013-02-13 09:22:42","username":"somesuername","password":"","role":"somerole","token":null,"token_expiration":null}}

So the api works. When I execute this:

this.model.id = 1;
this.model.fetch({ success: function(user){
                        console.log(user);    
                   }}); // this.model is instance of my model

I get in console:

d {cid: "c4", attributes: Object, _changing: false, _previousAttributes: Object, changed: Object…}
_changing: false
_pending: false
_previousAttributes: Object
attributes: Object
    __proto__: Object
changed: Object
cid: "c4"
id: Array[1]
__proto__: e

So the result was successful by model didn't fetch any data - am I missing something?

Upvotes: 0

Views: 88

Answers (1)

jevakallio
jevakallio

Reputation: 35970

Backbone.Model stores the data inside the attributes property. From Backbone documentation:

The attributes property is the internal hash containing the model's state — usually (but not necessarily) a form of the JSON object representing the model data on the server. It's often a straightforward serialization of a row from the database, but it could also be client-side computed state.

Please use set to update the attributes instead of modifying them directly.

Try expanding the attributes node in the console object inspector, and you should see your data.

Upvotes: 2

Related Questions