davibq
davibq

Reputation: 1099

Backbone Model: empty attribute with get method but in attributes object

I have these lines:

console.log(JSON.stringify(this.model.attributes));
console.log(this.model.get('name'));

And this is the output:

"{"name":"ffg","key":"1c277f82-f093-d359-4cfb-febe4614a3b1"}"
""

I'm starting with Backbone. Any ideas why the object is in the attributes but it returns empty with the get method??

EDIT:

This shouldn't affect answers, but I'm working with Phone Gap.

The 2 console.log are lines next to each other.

EDIT 2:

var temp = _(this.model.attributes).clone();
console.log(JSON.stringify(temp));
console.log(temp.name);

This logs:

{"name":"ss","questions":[],"order":0,"key":"5c35c304-4863-02c0-4d18-101c655aa4ae"}
""

Upvotes: 0

Views: 740

Answers (2)

zpawn
zpawn

Reputation: 1

When you fetch model, try set this yourModel.fetch({async: false})

Upvotes: 0

QuantumLicht
QuantumLicht

Reputation: 2183

I would consider logging this.model and inspecting the object into a browser (e.g. Chrome or Firefox) then look into the attributes property of the object. Make sure that the name property is defined as you intended. Also, please make sure that no event is triggered that could modify your model object.

Upvotes: 1

Related Questions