user2206964
user2206964

Reputation: 1

backbone model, get method does not work

I'm just a beginner in backbone. And I have a little problem. I fetch my collection and find model:

ads = new Somecar.Collections.Ads()
ads.fetch()
ad = ads.first()

But when I trying

ad.get('color')

backbone returns 'undefined', but at the same time

ad.attributes.ad.color

returns 'black'. What's wrong? How I can read model attribute through get method? Thanks

Upvotes: 0

Views: 382

Answers (2)

Alexander Beletsky
Alexander Beletsky

Reputation: 19841

I think @Herman Tran is right, about parse: function. Besides, if you what to access you collection models after fetch, you should write:

collectction.fetch({
  success: function (collection) {
    var model = collection.first();
    model.get('attr);
  }
});

Since, fetch is async and it's not guranteed that collection is ready while you access it.

Upvotes: 0

Herman Tran
Herman Tran

Reputation: 1591

Seems like you would need ad.get('ad').color based on your attributes property. Perhaps you could do this parsing in your collection so that the model's color attribute is directly set when fetching the collection:

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

Upvotes: 1

Related Questions