2013-02-27 61 views

回答

0

如果我是對的,會說你想從集合中刪除一些項目,點擊一個按鈕。

因此,我們必須在kb的觀點:

var viewModel = kb.ViewModel.extend({ 
    constructor: function(model, options) { 
     var self = this 
     this.delete= function(){ 

      self.coll.delete(self) 
     } 
     this.coll = options.coll 
     this.name = kb.Observable(model, {key: 'name'}) 

    } 
}); 

var yourCollection = new Backbone.Collection(); 
var yourModel = new Backbone.Model({name: 'Stefan'}); 
var yourKBView = new viewModel (yourModel, {coll: yourCollection}); 

這是存儲一些嵌套內容的簡單方法。

當您將在集合中添加模型時自動執行此操作時,您可以像這樣覆蓋視圖的創建功能。

var collectionViewModel = kb.ViewModel.extend({ 
     constructor: function(collection, options) { 
      var self = this 
      this.coll= kb.collectionObservable(collection, { 
       /** 
       * Calls by adding a model to the collcetion 
       * @param model - 
       * @param options - 
       * 
       */ 
      create: function(model, options){ 
       var options = options || {} 
       options.coll = self  
       return new viewModel(model,options) 
      } 
     }); 
    } 
});