I’m working on a backbone project with underscore (1.3.0). Underscore has a shuffle method that shuffles
create a collection
countries = new Countries
child
check length
countries.length
0
fetch the data (using an underscore method, fetch)
countries.fetch();
Object
XHR finished loading: "http://localhost:3000/countries". jquery.js:8215
Now it has a length of four
countries.length
4
try to return a randomly shuffled array
countries.shuffle();
TypeError: Object [object Object] has no method ‘shuffle’
shuffle otherwise works in my environment
_.shuffle([1, 2, 3, 4, 5, 6]);
[2, 1, 4, 5, 6, 3]
I watched Ryan Bates backbone railscast, and he essentially does the same thing but his works. One difference, though, was when he created his Collection object, it had a different return value
entries = new Raffler.Collections.Entries()
entries #return value
However, when I create a collection, it returns ‘child’
countries = new Countries
child
But I don’t see that making a difference because I’m still able to call countries.fetch(); which is an underscore method.
Any suggestions?
You might be using an old version of Backbone, or a version of underscore that’s not compatible with your version of Backbone.
The first version of Backbone to include the
Collection.shufflemethod was 0.9.0. The documentation in those days did not specify the minimum version for underscore, but for Backbone 0.9.1 the minimum was> 1.3.1. For the current Backbone release (0.9.9) it’s> 1.4.3.Also,
fetchis not an underscore method.