使用主干批量删除

时间:2013-12-07 19:31:17

标签: backbone.js

我有一个包含员工列表的视图,最终用户选择员工并删除多个员工。

列表的每一行都包含一个复选框。最终用户选中多个复选框,然后按删除按钮。所选记录需要删除。

MVC控制器负责删除部分。删除方法的签名是:

DeleteEmployes(List<int> empIds).

我怎样才能做到这一点?

我的骨干模型是:

var emp = Backbone.Model.extend({
defaults:{
             Id:null,
             fname:null,
             lname:nulll.
}
});

2 个答案:

答案 0 :(得分:1)

为了删除具有一个请求的所有模型,您需要使用一种方法扩展骨干的集合,该方法将HTTP DELETE请求发送到使用“DeleteEmployes(List empIds)”函数的控制器操作。这样的事情可能会成功。

Backbone.Collection.prototype.bulk_destroy = function() {
  var modelId = function(model) { return model.id };
  var ids = this.models.map(modelId);
  // Send ajax request (jQuery, xhr, etc) with the ids attached
  // Empty the collection after the request
  // You may want to include this as a success callback to the ajax request
  this.reset(); 
};

答案 1 :(得分:0)

创建一个Backbone Collection并循环遍历它,破坏每个模型。这会将每个模型的DELETE命令发送到服务器。

var Employees = new Backbone.Collection([
 { name: 'Employee1' }, 
 { name: 'Employee2' }, 
 { name: 'Employee3' }, 
]);

Employees.each(function(model){
  model.destroy();
});