SHT
SHT

Reputation: 720

BackboneJS - How to limit number of models in collection

How can I set a limit to the number of models to get displayed in collection?

So far I have:

var myCollection = new Collection();
myCollection.url = this.model.url() + '/feed';
myCollection.fetch();

I have tried to add:

myCollection.first(20);

but that didnt work, since the first() only works on arrays?

Upvotes: 0

Views: 470

Answers (1)

Evgeniy
Evgeniy

Reputation: 2921

You can use .parse() method. It called with .fetch() and helps to modify server response. In your case in may be helpful to slice models array you get from backend.

var myCollection = Backbone.Collection.extend({
    parse: function(response) {
        return response.slice(0,20);
    }
});

On every fetch you will have first 20 models in your collection.

Upvotes: 2

Related Questions