Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone - How can I slice a collection?

Tags:

backbone.js

I've got a Backbone Collection. How can I slice the collection, or at least truncate the list to a certain length?

like image 972
a paid nerd Avatar asked Feb 03 '12 00:02

a paid nerd


1 Answers

Assuming that you have your collection defined and initialized and that you want to mutate the collection (change it in place) you have to do:

collection.reset(collection.first(n));

of you can use .last(n) to get last N elements.

If you just wanted to get the first n elements without modyfying the collection just do:

var models = collection.first(n);

Here is a list of all the underscore methods you can use directly on your collection.

like image 170
Tom Tu Avatar answered Oct 13 '22 05:10

Tom Tu