Question

I'm new to Backbone.js. I'm intrigued by the idea that you can just supply a URL to a collection and then proceed to create, update, delete, and get models from that collection and it handle all the interaction with the API.

In the small task management sample applications and numerous demo's I've seen of this on the web, it seems that the collection.fetch() is used to pull down all models from the server then do something with them. However, more often than not, in a real application, you don't want to pull down hundreds of thousands or even millions of records by issuing a GET statement to the API.

Using the baked-in connection.sync method, how can I specify parameters to GET specific record sets? For example, I may want to GET records with a date of 2/1/2014 or GET records that owned by a specific user id.

In this question, collection.find is used to do this, but does this still pull down all records to the client first then "finds" them or does the collection.sync method know to specify arguments when doing a GET to the server?

Was it helpful?

Solution

You do use fetch, but you provide options as seen in collection.fetch([options]).

So for example to obtain the one model where id is myIDvar:

collection.fetch(
{
    data: { id: myIDvar },
    success: function (model, response, options) { 
        // do a little dance;
    }
};

My offhand recollections is that find, findWhere and where would invoke all models being downloaded and then the filtering taking place on the client. I believe with fetch the filtering takes places on the server side.

OTHER TIPS

You can implement some kind of pagination on server side and update your collection with limited number of records. In this case all your data will be up to date with backend.

You can do it by overriding fetch method with you own implementaion, or specify params For example:

collection.fetch({data: {page: 3})

You can also use find where method here

collection.findWhere(attributes)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top