Friday 15 August 2014

backbone.js - Backbone Collection get property -



backbone.js - Backbone Collection get property -

got server returning json object so:

{ 'key1':'value' 'key2':{ 'key2_0':'value' } }

and collection:

var collection = backbone.collection.extend({ url:api.url//which returns object above }); var collection = new collection(); collection.fetch({ success:function(data){ //do } });

now need utilize properties of collection throughout application, need key1, have collection.at(0).get('key1');//returns 'value', because info returned stored within collection, in new array @ key 0.

question:

how directly... collection.get('key1')//now returns undefined... because is.

i know expose object global scope in collection success function some_other_var = data.tojson()[0] , access some_other_var properties directly, that's not i'm looking for;

in order utilize get() function backbone.collection need know model id or cid wanted.

for instance, lets info coming server follow:

[{ id: '123', name: 'alex' }, { id: '456', name: 'jhon' }]

in case can this:

this.collection.get('123').get('name') // homecoming "alex"

keep in mind collection set of model, behind scenes doing collection.get() getting model

tip: if don't have kind of id in server data, there alternative of using underscore methods:

find filter some contains etc

backbone.js

No comments:

Post a Comment