如何在特定索引中将模型插入到backbone.js 集合中? [英] How do I insert a model into a backbone.js collection in a specific index?

查看:21
本文介绍了如何在特定索引中将模型插入到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.

到目前为止我尝试过的:

What I tried so far:

我在集合页面"中添加了一个页面"模型,然后尝试通过更改它们的顺序来交换它们:

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://backbonejs.org/#Collection-comparator

这篇关于如何在特定索引中将模型插入到backbone.js 集合中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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