如何在集合中移动模型?
假设我有一个普通的 Backbone.Collection,其中包含一些模型:
var Library = Backbone.Collection.extend({
model: Book
});
lib = new Library(
[Book1, Book2, Book3, Book4, Book5, Book6]
]);
如何将集合中的模型移动 - 例如,将第 5 个模型移动到第 2 个位置?因此,无需按模型字段排序,而只需手动更改排序顺序。
注意:我简化了模型 Book1, ...
。它们当然是Backbone.Model
。
Say I'm having a plain Backbone.Collection
with some models in it:
var Library = Backbone.Collection.extend({
model: Book
});
lib = new Library(
[Book1, Book2, Book3, Book4, Book5, Book6]
]);
How can I move a model within a collection - e.g. the 5th one to the 2nd position? So no sorting by a model field but just changing the sort order manually.
Note: I simplified the models Book1, ...
. They are of course Backbone.Model
s.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以直接访问模型数组来修改顺序。大致基于这个问题 从一个数组移动数组元素位置到另一个,这样的东西应该可以工作:
和一个演示http://jsfiddle.net/nikoshr/5DGJs/
You can directly access the array of models to modify the order. Loosely based on this question Move an array element from one array position to another, something like this should work:
And a demo http://jsfiddle.net/nikoshr/5DGJs/