Knockback:从可观察集合中删除项目

时间:2013-02-27 14:47:14

标签: backbone.js knockback.js

给定Knockback中的可观察集合,如何从基础集合中删除项目以响应knockout.js点击事件?

1 个答案:

答案 0 :(得分: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)
            }
        });
  }
});