簡體   English   中英

骨干模型,get方法不起作用

[英]backbone model, get method does not work

我只是骨干的初學者。 我有一個小問題。 我獲取我的收藏並找到模型:

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

但是當我嘗試

ad.get('color')

主干返回“未定義”,但同時

ad.attributes.ad.color

返回“黑色”。 怎么了? 如何通過get方法讀取模型屬性? 謝謝

似乎您需要根據您的attributes屬性獲得ad.get('ad').color 也許您可以在集合中進行此解析,以便在獲取集合時直接設置模型的color屬性:

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

我認為@Herman Tran是正確的,關於parse:函數。 此外,如果要在獲取后訪問集合模型,請輸入以下內容:

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

由於fetch是異步的,因此在您訪問它時不保證收集已准備就緒。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM