动态创建当前集合的实例
首先,对标题表示歉意 - 如果有人在阅读问题后有更好的版本,请编辑或询问我。
我使用“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不是 100% 确定你在问什么,但我认为你想在集合的实例上运行“where”并取回一个新的集合。只是在萤火虫中玩耍并想出了这个:
`
这可能可以做得更好..但这似乎很实用。
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:
`
This could probably be done better.. but this seems functional.