我要如何插入一个模型转换为Backbone.js的集合在一个特定的指数? [英] How do I insert a model into a backbone.js collection in a specific index?

查看:91
本文介绍了我要如何插入一个模型转换为Backbone.js的集合在一个特定的指数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在位置Collection.length-2插入一个模型到一个集合。集合中的最后一个模型应该永远留在集合中的最后一个模型。

I need to insert a model into a Collection at position Collection.length-2. The last model in the collection should always stay the last model in the collection.

我试过到目前为止:

我加一页模式向集页,然后试图通过改变其周围序列交换他们:

I added one "page" model to the Collection "Pages" and then tried to swap them around by changing their sequence:

var insertedpage = Pages.at(Pages.length-1);
var lastpage = Pages.at(Pages.length-2);
insertedpage.set({sequence: Pages.length-1});
lastpage.set({sequence: Pages.length});

我也试图删除的最后一页,然后添加一个新的页面,然后添加最后一页回。

I also tried to remove the last page, then add a new page and then add the last page back in.

var lastpage =  Pages.pop();
Pages.add({example1: example2});
Pages.push(lastpage);

这些都不奏效。新添加的页面仍显示在收藏作为最后的模型。我需要调用某种秩序的功能在此之后?

neither of these worked. The newly added page still appears as last model in the Collection. Do I need to call some kind of order function after this?

推荐答案

对不起,我简要回答(没有时间做出反应),但看看定义一个比较器功能。

Sorry for the brief answer (don't have time to respond), but look at defining a comparator function.

http://documentcloud.github.com/backbone/#Collection-comparator

这篇关于我要如何插入一个模型转换为Backbone.js的集合在一个特定的指数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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