从Backbone.Collection.remove(n) [英] retrieve element from Backbone.Collection.remove(n)

查看:222
本文介绍了从Backbone.Collection.remove(n)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Backbone.js执行我的第一步,其中一个涉及能够从集合中删除项目,更重要的是,检索该项目。 Backbone.Collection.remove方法简单地返回原始集合,并且项目已删除,因此目前我在删除之前获取对所需项目的引用:

  var Collection = Backbone.Collection.extend(... Backbone.Models ...数组),
removedItem = Collection.get(3);
console.log(Collection.remove(3)); //同一集合sans#3

我的问题是如果有一个短手方法检索删除项目?



编辑:JFTR,我已经读了一个公平的源,知道原始方法返回对集合的引用 -

  remove:function(models,options){
// < snip为简洁>
//链模式传入
return this;
},



我似乎很奇怪,它没有返回删除的项目。 ,所以我只是想知道是否有另一种方法,我错过了,或一个通用的方式实现这种模式。这不是我第一次使用一个长的解决方法当API有一些秘密doohickey它的袖子...因为它是可能会扩展类。



<$ p

您可以向Backbone.Collection'type'添加一个函数,并在您创建的每个集合中使用removeModel。 $ p> Backbone.Collection.prototype.removeModel(model){
var _model = this.get(model);
this.remove(item);
return _model;
}

var removedModel = collection.removeModel(model);


I'm taking my first steps with Backbone.js, and one of those involves being able to remove an item from a collection, and more importantly, retrieve that item. The Backbone.Collection.remove method simply returns the original collection with the item removed, so at the moment I'm obtaining a reference to the desired item prior to removal:

var Collection = Backbone.Collection.extend(...array of Backbone.Models...),
    removedItem = Collection.get(3);
console.log(Collection.remove(3));//same collection sans #3

My question is if there is a short hand method for retrieving the remove item?

Edit: JFTR, I've read a fair bit of the source, and know that the original method returns a reference to the collection -

remove: function(models, options) {
  // <snip for brevity>
  // chain pattern incoming
  return this;
},

It seemed odd to me that it didn't return the removed item., so I was just wondering if there was another method I'm missing, or a common way of achieving this pattern. Wouldn't be the first time I've used a long workaround when the API had some secret doohickey up it's sleeve...as it is I'll probably extend the class.

解决方案

You could add a function to the Backbone.Collection 'type' and use removeModel on every collection you create.

Backbone.Collection.prototype.removeModel(model) {
    var _model = this.get(model);
    this.remove(item);
    return _model;
}

var removedModel = collection.removeModel(model);

这篇关于从Backbone.Collection.remove(n)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆