因此,無論何時我想從我的收藏中選擇一個模型,我總是會使用骨幹自己的where
方法。Backbone.js爲什麼我必須使用下劃線(_.each)來定位模型?
var homeTeam = this.collection.where({teamName: this.currentTeamName('home')});
我有我自己的方法,抓住當前和我的團隊通過「家」或「離開」作爲一個參數,它知道搶哪種模式,這是所有罰款和花花公子,另一個例子是隻傳遞一個像下面的字符串。
// This is practically the same as the above.
var homeTeam = this.collection.where({teamName: 'bulls'});
所以,如果我記錄變量console.log(homeTeam)
控制檯顯示該模型,就像它做任何模型。
控制檯顯示我,我有機會獲得常規方法的模型可以使用http://backbonejs.org/#Model但如果我調用這些方法,我得到一個錯誤之一,前homeTeam.save({someAttr: 'juicy'});
所以我只是用下劃線的each
方法如下圖所示,它作品。
_.each(homeTeam, function(model) { model.save({someAttr: 'juicy'}); }, this);
我一直在做這一段時間,但我很好奇,想知道爲什麼我必須做的是,記錄該homeTeam
當它通過模型就像
_.each(homeTeam, function(model) { console.log(model) }, this);
它們顯示完全一樣控制檯。
嘗試直接調用.save時會出現什麼錯誤? –
你們都好,現在它作爲一個數組返回是有道理的。讓我玩弄'findWhere'並挖掘brb,你們真棒。 –