Backbone.js如何在Collection中随机播放项目

mge*_*245 6 collections model shuffle backbone.js

在尝试随机化集合中对象的顺序时遇到问题.

这是我尝试过的代码:

console.log(this.collection);
shuffled = this.collection.shuffle();
console.log(shuffled);
Run Code Online (Sandbox Code Playgroud)

这是控制台输出(使用带有3个项目的测试集合):

child {models: Array[3], length: 3, _byId: Object, url: "/myurl/myid", _listenerId: "l7"…}
_byId: Object
_events: Object
_idAttr: "id"
_listenerId: "l7"
length: 3
models: Array[3]
__proto__: Surrogate

[child, child, child]
0: child
1: child
2: child
length: 3
__proto__: Array[0]
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,Collection并没有被正确地改组,而是创造了一个充满讨厌孩子的新的不可用对象.

我想要做的就是随机化模型在集合中出现的顺序,然后将其传递给视图进行显示(我正在创建一个名为"randomize"的按钮,需要随机化集合中项目的显示).我认为这将是一项简单的任务,但此时我正在考虑创建一个全新的模型并在服务器上进行随机播放.

任何帮助是极大的赞赏!

Vit*_*huk 8

console.log(this.collection);
this.collection.reset(this.collection.shuffle(), {silent:true});
console.log(this.collection);
Run Code Online (Sandbox Code Playgroud)