1

Given an observable collection in Knockback, how do I remove an item from the underlying collection in response to a knockout.js click event?

Petrus Theron
  • 27,855
  • 36
  • 153
  • 287

1 Answers1

0

If I'm right, a would say you want delete some item from the collection by clicking a button.

So we have the kb view:

  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}); 

This is a simple way to store some nested information.

When you will do this automatic when a model is add in the collection you can override the create function of the view like this.

   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)
            }
        });
  }
});
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Stefan B.
  • 374
  • 3
  • 13