More restfunctions on canjs model

≡放荡痞女 提交于 2019-12-07 15:22:33

问题


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'
}, {

});

回答1:


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?



来源:https://stackoverflow.com/questions/14387020/more-restfunctions-on-canjs-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!