Backbone.js How to mix items in a collection

I am having problems trying to randomize the order of objects in a collection.

Here is the code I tried:

console.log(this.collection); shuffled = this.collection.shuffle(); console.log(shuffled); 

And here is the console output (using a test collection with three elements):

 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] 

As you can see, the collection is not shuffled properly, instead it creates a new unused object full of ghostly children.

All I'm trying to do is randomize the order in which models appear in the collection before passing it to the view for display (I create a β€œrandomize” button that should randomize the display of elements in the collection). I thought this would be a daunting task, but at the moment I'm considering creating a completely new model and performing shuffling on the server.

Any help is much appreciated!

+6
source share
1 answer
 console.log(this.collection); this.collection.reset(this.collection.shuffle(), {silent:true}); console.log(this.collection); 
+8
source

Source: https://habr.com/ru/post/945846/


All Articles