prePEND模型集合 [英] Prepend models to collection

查看:114
本文介绍了prePEND模型集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一种优雅的方式,以prePEND模型集合添加它们时。在骨干源$ C ​​$ C有一个不印字的方法来做到这一点,但添加添加方法模型集合太复杂,简单地扩展。我建立一个幻灯片,取决于照片和微博,并应始终显示最新的顶部,最古老的最后一次。我的问题的简化:

I'm looking for an elegant way to prepend models to a collection when adding them. In the Backbone source code there is an unshift method to do just that, but the add method for adding models to a collection is too complex to simply extend. I'm building a slide show that depends on photos and tweets and should always show the newest on top, oldest last. Simplification of my problem:

初​​始化程序,API调用返回:

Initialize app, API call returns:

apple, banana, lemon

所以,苹果是最新的幻灯片。该集合构建的:

So, apple is the newest slide. The collection is built:

apple, banana, lemon

间隔后,API调用检查是否有新的水果,结果是:

After interval, API call checks if there are newer fruits, result:

orange

集合追加模型:

(apple, banana, lemon), orange

这不是我想要的东西;橙色是最新的水果,之后苹果。但是,在这种情况下,我可以追加和绘图幻灯片时再反向扭转之前我最初的收藏。现在,事情变得更加复杂(我加了括号澄清):

That's not what I want; orange is the newest fruit, and apple after that. But, in this case I could reverse my initial collection before appending and re-reverse when drawing slides. Now, things get more complex (I added parentheses to clarify):

API调用检查甚至更多的水果:

API call checks for even more fruits:

pineapple, kiwi

收集一个附加的新车型之一:

Collection appends new models one by one:

((apple, banana, lemon), orange), pineapple, kiwi

因此​​,从从新到旧,现在,我的收藏​​是这样的​​:

So now from new to old, my collection looks like this:

((not so old, older, oldest), pretty new), brand new, new

这使得完全没有意义的:)

Which makes absolutely no sense :)

Ofcourse,改变了API响应可以让​​我的生活更轻松,但是这是目前不是一个选择;同时,它不应该真正的问题是项目的反应是新订单 - >老而由我是罚款

Ofcourse, changing the API response could make my life easier, but that's currently not an option; also, it shouldn't really matter as the order of items in the response is new -> old which is fine by me.

我无法找到一个出这个现成的解决方案,而我不希望不印字新车型克隆骨干源的一部分刚来的东西过于复杂。建议?谢谢!

I couldn't find an out of the box solution for this and I don't want to overcomplicate things by cloning part of the backbone source just to unshift new models. Suggestions? Thanks!

推荐答案

如果该模型有某种日期或时间与它们相关的或任何其他类型的排序(您可能会手动添加一个,如果需要的话)可以排序收集通过实施比较功能您收藏。参见:

If the models have some kind of date or time associated with them or indeed any other kind of ordering (you can potentially add one manually if needed) you can sort the collection by implementing the comparator function in your collection. See:

http://backbonejs.org/#Collection-comparator

这篇关于prePEND模型集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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