1

my Collection fetches a few records and I have to show only top 10 records from it. I attempted

   this.collection.each(function(){
        if (count == 10) break;
        //pass model to view
   });

unfortunately break does not work with each() API of underscore.js Refer here: how to break the _.each function in underscore.js

How do I write a filter to pluck only top 10 from the collection

     this.collection.filter();

UPDATE: collection.first(10) fetched me filtered list. However, I still needed to chain .each() to this collection to process the collection items. collection.first() does not allow chain. Please refer to my selected answer for solution to this.

Community
  • 1
  • 1
Vikram
  • 4,162
  • 8
  • 43
  • 65
  • possible duplicate of [get only the first 20 items in a backbone collection](http://stackoverflow.com/questions/8240382/get-only-the-first-20-items-in-a-backbone-collection) – mu is too short Aug 09 '12 at 20:17
  • @muistooshort I see this is an exact duplicate.Thanks for pointing it out. I have received partial solution to my question. I am amending this question so that I can get help on the rest of it – Vikram Aug 09 '12 at 20:54

1 Answers1

7

E.g.

this.collection.first(10)

Then, if you need to work with each model, e.g. :

    var collection = new Backbone.Collection([{id:1}, {id:2}, {id:3}, {id:4}, {id:5}],{model: Backbone.Model});

    var newCollection = new Backbone.Collection(collection.first(2));

    newCollection.each(function(model){
      alert(JSON.stringify(model.toJSON()));
    });

See the jsfiddle. ​ Note that there is another way to do it with the Underscore chain method as said in this topic.

Have a look at Backbone doc and Underscore doc.

Community
  • 1
  • 1
xiris
  • 351
  • 1
  • 3
  • 17
  • first() in underscore.js is array function and this is a Collection. I tried using it and browser throws up at that very line saying: Object doesnt support this property or method...thanks for your answer! – Vikram Aug 09 '12 at 20:02
  • 1
    Anyway, it should work for a Collection. There is another topic on stackoverlow : http://stackoverflow.com/questions/8240382/get-only-the-first-20-items-in-a-backbone-collection. A lot of underscore methods also work for Backbone Collection, just removing the first argument (array) and calling the method from the Backbone Collection object. – xiris Aug 09 '12 at 20:12
  • 1
    I second xiris, take a look at this fiddle (look in the console) http://jsfiddle.net/RgBVw/ – Jens Alm Aug 09 '12 at 20:22
  • @JensAlm I was trying to do: collection.first(2).each(function(model){ alert(model.toJSON()); }); but it throws me that error: http://jsfiddle.net/RgBVw/3/ – Vikram Aug 09 '12 at 20:42
  • @xiris thanks...I see from your link that it does not allow chaining.. which is why I am getting that error – Vikram Aug 09 '12 at 20:43
  • Else, you can use the Underscore chain method (http://documentcloud.github.com/underscore/#chain) as said in the other topic. – xiris Aug 09 '12 at 20:53
  • @xiris excellent! that works for me...Can you please edit your answer to incorporate the solution you provided in the fiddle. I will mark your solution as answer to my question....thanks for following up! – Vikram Aug 09 '12 at 20:56