有一個服務器返回JSON對象,像這樣:骨幹集合獲取屬性
{
'key1':'value'
'key2':{
'key2_0':'value'
}
}
和收集:
var Collection = Backbone.Collection.extend({
url:api.url//which returns the object above
});
var collection = new Collection();
collection.fetch({
success:function(data){
//do something
}
});
現在我需要在我的應用程序中使用收集的某些屬性,但說我需要key1
,我總是必須做collection.at(0).get('key1');//returns 'value'
,因爲返回的數據存儲在集合中,在一個新的數組中鍵爲0.
問題:
如何直接... collection.get('key1')//now returns undefined... because it is
。
我知道我可以在收集成功函數some_other_var = data.toJSON()[0]
中公開一個對象到全局範圍,並直接訪問some_other_var
屬性,但這不是我要找的;
你能創建一個演示問題的小提琴嗎? – 2014-11-06 06:29:07