Martin Erlic
Martin Erlic

Reputation: 5667

How can I limit the number of objects fetched from a Parse.com collection (Javascript)?

Can I do anything from here to limit the number of objects that are returned? By default, it is 100 and I don't need that many for now. Anyway to just fetch the most recent 10 objects in that class?

var Blog = Parse.Object.extend("Blog");

var Blogs = Parse.Collection.extend({
    model: Blog
});

var blogs = new Blogs();

blogs.fetch({
    success: function(blogs) {
        var blogsView = new BlogsView({ collection: blogs });
        blogsView.render();
        $('.main-container').html(blogsView.el);
    },
    error: function(blogs, error) {
        console.log(error);
    }
});

Upvotes: 0

Views: 109

Answers (2)

Martin Erlic
Martin Erlic

Reputation: 5667

I did it like this:

Blogs = Parse.Collection.extend({
    model: Blog,
    query: (new Parse.Query(Blog)).descending('createdAt').limit(10),
}),

...and showed the newest objects first by sorting by descending order.

Upvotes: 0

Ilya
Ilya

Reputation: 5557

From the backbone fetch doc:

jQuery.ajax options can also be passed directly as fetch options, so to fetch a specific page of a paginated collection: Documents.fetch({data: {page: 3}})

For setting the number of objects returned, the option name is limit, so:

blogs.fetch({ data: {limit: 10}, ...

Upvotes: 1

Related Questions