我如何可以移动一个集合中的一个模式? [英] How can I move a model within a collection?

查看:98
本文介绍了我如何可以移动一个集合中的一个模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个简单的 Backbone.Collection 在它的一些车型:

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]
]);

我如何移动集合中的模型 - 例如, 5日一到第二位?因此,没有一个模型字段排序,但只是手动更改排序顺序。

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.

注:我简化模型第一册,... 。他们是当然的 Backbone.Model 秒。

Note: I simplified the models Book1, .... They are of course Backbone.Models.

推荐答案

您可以直接访问模型的阵列修改订单。松散的基础上这个问题的<一个href=\"http://stackoverflow.com/questions/5306680/move-an-array-element-from-one-array-position-to-another\">Move从一个阵列位置到另一个数组元素,这样的事情应该工作:

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:

var c = new Backbone.Collection([{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}]);
console.log(c.pluck("id"));

var from_ix = 4,
    to_ix = 1;
c.models.splice(to_ix, 0, c.models.splice(from_ix, 1)[0]);
console.log(c.pluck("id"));

和演示 http://jsfiddle.net/nikoshr/5DGJs/

这篇关于我如何可以移动一个集合中的一个模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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