We have built a RESTful API with URLs like
/api/v1/cars/
/api/v1/cars/34
All these endpoints accept GET, PUT, POST and DELETE methods as usual.
To help you understand our models: these are cars whose engine/wheels/specifications/etc are typically modified as part of a process of tuning.
We therefore have the concept of a 'report' on a car: a collection of results of various tests that are carried out on a car and stored for later viewing (just like your mechanic might produce a report on your car for later comparison).
These reports are stored at URLs like
/api/v1/car_reports/
/api/v1/car_reports/76
We have chosen to not allow these end points to accept a POST method, you can only GET or DELETE them (there is no sense in a PUT because these reports should never be modified after creation).
We made the decision that you create a car report via the URL of the car itself:
- you POSTto an endpoint like/api/v1/cars/34/make_report, with a list of the tests you wish to run as the body data in this request,
- the back end uses the car's details together with the list of tests to produce the report which it stores at a URL like /api/v1/car_reports/77.
- the back end concludes by sending the location /api/v1/car_reports/77in the response to the client (and we decided, as a formality/convenience, to actually include a copy of the report in the body of the response).
Our front end consumes this API with a Backbone.Model and Backbone.Collection like:
var Car = Backbone.Model.extend();
var CarCollection = Backbone.Collection.extend({
    model: Car,
    url: '/api/v1/cars'
});
var CarReport = Backbone.Model.extend();
car CarReportCollection = Backbone.Collection.extend({
    model: CarReport,
    url: '/api/v1/car_reports'
});
We can create a Car easily via the CarCollection:
var car_collection = new CarCollection();
var car = car_collection.create({
    make: 'Ford',
    engine_size: 1600,
    tyres: 'Michelin'
});
// Suppose that car.id = 34
What is the best way to create a CarReport on that car?
At the moment I am doing an explicit ajax call like:
var tests = {
    'maximum speed',
    'miles per gallon'
};
$.ajax({
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify(tests),
    url: car_collection.url + '/' + car.id + '/make_report'
});
This feels like a hack.
How can I do this in a more Backbone-ish way?
Many thanks.
 
     
     
    