I have an existing RESTful API wich can't be changed. Now, i'm working on the webclient.
I would like to use a library like backbone.js (for the first time).
Here is the design of the existing API
1.) GET /persons - Returns the whole collection of persons
2.) GET /persons?p1=a&p2=b (e.g.) - Returns a subset of the complete collection which matches the criteria specified by the query parameters
3.) GET /persons/[id] - Returns a person by id
4.) POST /persons - modifies the collection (e.g. creates a new person) and returns the specific result
The problem with #1 (and #2): The output format is like this: {size: 1, persons: [{'id': 1, 'firstname': 'foo', 'lastname': 'bar'}]}
The problem with #2: how to map such a requst to backbone.js?
Any suggestions?
For #1, you would override parse. For example, in your Persons collection you would put the following method:
parse: function(response) {
return response.persons;
}
You are basically instructing the collection which attribute the models can be found in ('persons' in our case).
For #2, (as previously mentioned) it looks like you'll need to update sync. Hunter provided a good link in his response that should be really helpful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With