动态创建当前集合的实例

发布于 2024-11-08 06:59:57 字数 557 浏览 0 评论 0原文

首先,对标题表示歉意 - 如果有人在阅读问题后有更好的版本,请编辑或询问我。

我使用“where”方法扩展了核心 Backbone Collection 对象,该方法允许我对集合内的模型执行 _.select。目前,这会返回一个包含模型的新的普通 Collection 对象。我想要的是该方法返回与我调用的类型相同的 Collection 对象...

Backbone.Collection.prototype.where = function(a) {
  var execute = function(item) {
    ...
  };

  return new Backbone.Collection(this.select(execute));
};

var Accounts = Backbone.Collection.extend({...})

我想要的返回语句是返回一个新的 Account 集合。但我不想在我扩展的每个集合中定义或扩展此方法。像下面的伪代码一样:

return new instanceof this(this.select(execute));

有意义吗?

Firstly, apologies for the title - if anyone has a better version after reading the question, please edit or ask me to.

I have extended the core Backbone Collection object with a 'where' method that allows me to perform an _.select on the models within the collection. Currently this returns a new vanilla Collection object containing the models. What I want is for the method to return a Collection object of the same type as I am calling ...

Backbone.Collection.prototype.where = function(a) {
  var execute = function(item) {
    ...
  };

  return new Backbone.Collection(this.select(execute));
};

var Accounts = Backbone.Collection.extend({...})

What I want, at the return statement is to be returning a new Account collection. But I don't want to have to define or extend this method in each collection I extend. Something like the following pseudo code:

return new instanceof this(this.select(execute));

Make sense?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁梦 2024-11-15 06:59:57

我不是 100% 确定你在问什么,但我认为你想在集合的实例上运行“where”并取回一个新的集合。只是在萤火虫中玩耍并想出了这个:

var Chapter  = Backbone.Model;
var chapters = new Backbone.Collection;

chapters.comparator = function(chapter) {
  return chapter.get("page");
};

chapters.add(new Chapter({page: 9, title: "The End"}));
chapters.add(new Chapter({page: 5, title: "The Middle"}));
chapters.add(new Chapter({page: 1, title: "The Beginning"}));

Backbone.Collection.prototype.where = function(selector) {
 var newCol = new this.__proto__.constructor();
 var itemsToInsert = this.select(selector);
 itemsToInsert.forEach(function(item){ newCol.add(item) });
 return newCol;
};

chapters.where(function(c){ return c.get('page') == 1 });

`

这可能可以做得更好..但这似乎很实用。

Im not 100% sure of what you are asking, but I think you want to run a 'where' on an instance of a collection and get back a NEW collection. Was just playing around in firebug and came up with this:

var Chapter  = Backbone.Model;
var chapters = new Backbone.Collection;

chapters.comparator = function(chapter) {
  return chapter.get("page");
};

chapters.add(new Chapter({page: 9, title: "The End"}));
chapters.add(new Chapter({page: 5, title: "The Middle"}));
chapters.add(new Chapter({page: 1, title: "The Beginning"}));

Backbone.Collection.prototype.where = function(selector) {
 var newCol = new this.__proto__.constructor();
 var itemsToInsert = this.select(selector);
 itemsToInsert.forEach(function(item){ newCol.add(item) });
 return newCol;
};

chapters.where(function(c){ return c.get('page') == 1 });

`

This could probably be done better.. but this seems functional.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文