2
A
回答
1
好了,我想出了以下方法:
假設模型有一個屬性status
,當它被修改爲active
我想隱藏在其他條目的編輯按鈕(或只需禁用它)。
我收藏視圖監聽模型中的變化:
initialize: function(){
this.listenTo(this.collection, "change:status", this.triggerEditable);
},
的偵聽器回調看起來像這樣:
triggerEditable: function(obj){
var triggerValue = null;
// I am interested in a status which became 'active' or stopped being 'active'
if (obj.get("status") == 'active' && obj.previous("status") != 'active') {
triggerValue = "editable:false";
} else if (obj.get("status") != 'active' && obj.previous("status") == 'active') {
triggerValue = "editable:true";
}
// for any other status change - return
if (!triggerValue) return;
// trigger is fired for all other objects in the collection
_.each(obj.collection.without(obj),function(otherObj) {
otherObj.trigger(triggerValue);
});
}
所以,當一個對象被激活或停止活躍,edidable:false
或edidable:true
是爲所有其他條目觸發的。所有我需要做的是添加到模型視圖初始化監聽器:
this.listenTo(this.model, "editable:false", this.disableEdit);
this.listenTo(this.model, "editable:true", this.enableEdit);
在這裏,我想我可以兩行合併爲一個,先聽editable
命名空間(如何?)和然後通過傳遞一個參數給函數(又是如何?)。
從這裏是直線前進 - 實現偵聽器回調:
disableEdit: function() {
var e = this.$el.find('button.edit')
e.attr('disabled','disabled');
}
如果有人有什麼要補充的,或者使這種解決方案更好,我會很高興地聽到。 無論如何,希望它會對別人有幫助!
2
您可以在默認設置爲true
的模型上添加屬性editable
。然後當您點擊其中一個視圖上的「編輯」按鈕時,您可以遍歷其他視圖的所有模型,並將editable
設置爲false
。在視圖上,您將聽取模型更改並重新渲染視圖。如果editable
是false
,您可以禁用編輯按鈕。
相關問題
- 1. 骨幹集合 - 如何過濾與其他集合的集合?
- 2. 在骨幹集合中訪問模型
- 3. 骨幹集合模型不可訪問
- 4. 骨幹:Singleton的意見?
- 5. 骨幹js集合問題
- 6. 如何骨幹意見
- 7. 嵌套意見骨幹JS
- 8. 用骨幹交換意見?
- 9. 訪問嵌套的骨幹收集
- 10. 集合中的骨幹集合
- 11. 集合中的骨幹集合
- 12. 骨幹訪問數據/其他視圖的事件
- 13. 骨幹集合導出問題
- 14. 隨着骨幹 - 如何訪問從JSON集合中的模型
- 15. 如何訪問骨幹集合的獲取結果?
- 16. 迭代骨幹集合
- 17. 骨幹找到集合
- 18. 骨幹:預取集合
- 19. 骨幹集合推環
- 20. 修改骨幹集合
- 21. 骨幹 - 渲染集合
- 22. 骨幹JS集合解析
- 23. 骨幹嵌套集合
- 24. 預加載骨幹集合
- 25. 骨幹多個集合取
- 26. 骨幹篩選集合
- 27. 骨幹定義集合
- 28. 骨幹共享集合
- 29. 骨幹意見不能與提取json
- 30. Require.js加載骨幹意見,以便
不錯的一個!是否可以添加到數據庫中不存在的模型屬性?怎麼樣? – guyaloni
說實話,我從來沒有將我的骨幹模型同步到服務器。我無法對客戶端模型進行映像,因此不允許擁有自己的屬性。你可以有一個自定義的[sync](http://backbonejs.org/#Model-sync)方法實現,但這可能不是最漂亮的解決方案。 – rednaw
也許:http://stackoverflow.com/questions/11941010/client-side-only-attributes-in-backbone – rednaw