Question

Is there a way to add more rest bindings on model then just the four CRUD functions?

var Customer = can.Model({
    findAll: 'GET /customer',    
    findOne: 'GET /customer/{id}',
    create: 'POST /customer',
    update: 'PUT /customer/{id}',
    destroy: 'DELETE /customer/{id}'
    //maybeAnOtherMethod: 'PUT /customer/{id}/activate'
}, {

});
Was it helpful?

Solution

The idea behind REST is that you have resources and actions on these resources. The resource itself is described by a URL, the action is described by an http verb.

Hence, GET is the action for reading, /customer/{id} describes the resource you want to load. This is fine. And so, all of your five methods are fine, because can.Model is designed to handle CRUD functionality with a basic REST interface.

The problem with the last (commented) one is that you mix the resource and the action inside the url. "activate" is definitely a verb and no resource. Hence it does not belong there, but it should be an http verb. This is why you don't find support for this way of programming a REST interface - simply because it's not REST.

While you could redesign the API to think of activate as a resource, it's more likely that a customer's status as activated or not activated is a part of the customer resource. You would use the update method, as you are changing something about the customer. In CanJS, it would look something like this:

Customer.findOne({id: 5), function( customer ){
  customer.attr('active', true);
  customer.save();
}

To cut a long story short: In REST, the urls are all about nouns, the http methods are all about verbs.

Does this help to make things a little bit clearer?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top