javascript - How to map a RESTful API to an URL in BackboneJS model and collections? -


i have following restful api:

get /club/1.0/club.json?sport=fo&country=es

this service gives list of clubs sport , country (ex: football in spain)

get /club/1.0/club.json?id=xxxx

this service gives details of club

in backbone.js, try following urls collection::

http://localhost:3000/club/1.0/club.json,

questions:

  1. how can deal versioning ?
  2. how can specified required parameters sport , country ?

for model use urlroot :

http://localhost:3000/club/1.0/club.json,

i think 1 easy because save method add /:id urlroot work expressjs server.

thank answer(s).

you should set using backbone sync in collections/models. example:

collection:

var clubcollection = backbone.collection.extend({   model: clubmodel,   sync: function(method, collection, options){      options = _.extend({}, options, {url: 'club/1.0/club.json');     return $.ajax(options);   } }); 

model

var clubmodel = backbone.model.extend({   sync: function(method, model, options){      options = _.extend({}, options, {url: 'club/1.0/club.json');     switch (method) {       case 'read':    options.data = _.extend({}, options.data, {         id: model.id        });   break;   // case 'delete':   // break;   // case 'update':   // break;   // case 'create':   // break;    }    return $.ajax(options);   } }); 

then make request using yourcollection.fetch({data:{sport:'fo',country:'es'}}). , model can mymodel.set('id', 1).fetch().

update: oh, , versioning, can use variable instead of number directly.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -