Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone + RESTful API

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

Issues:

  • 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?

like image 979
kalamar Avatar asked Jun 13 '26 00:06

kalamar


1 Answers

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.

like image 54
robmisio Avatar answered Jun 14 '26 12:06

robmisio