在Knockback中给定一个可观察集合,我如何从底层集合中删除一个项目以响应knockout.js单击事件?
如果我是对的,a会说您想要通过单击按钮从集合中删除一些项目。
所以我们有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});
这是存储一些嵌套信息的简单方法。
当一个模型被添加到集合中时,你可以像这样覆盖视图的create函数。
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)
}
});
}
});