laetiporous
laetiporous

Reputation: 80

How to limit a Backbone collection using a query?

Say I have a query implemented on my REST server which limits the number of items I get from a resource through a query string like /items?startswith=Foo. How do I hook this up to backbone so that Items collection changes when a different query is specified in the browser. Do I change the url parameter of the collection in response to a change in input and then fetch. Not looking for anyone to write code for me; I am just new to Backbone need someone to nudge me in the right direction.

Upvotes: 0

Views: 122

Answers (1)

Eugene Glova
Eugene Glova

Reputation: 1553

In official backbone documentation there is a note about this:

http://backbonejs.org/#Collection-fetch

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}})

So you can use data attribute while fetching your Items collection like this:

Items.fetch({
    data: {
        startswith: "Foo"
    }
});

So you don't need to change your model url in this case

Happy backbone coding!

Upvotes: 1

Related Questions