开发者

How to generate seperate RESTful urls for edit and update in Backbone js model?

I'm following Jame Yu's Backbone tutorial here to create my own app. Below is my model. I wonder if there's a way to generate separate urls for edit and update (RESTful) instead of just 1 as in the tutorial. I use Rails on the back end. Thanks.

var BusinessCard = Backbone.Model.extend({

  url : function() {

    var base = 'business_cards';

    if (this.isNew()) return 开发者_StackOverflow社区'backbone/' + base;

    return 'backbone/' + base + (base.charAt(base.length = 1) == '/' ? '' : '/') 
    + this.id;

  }
})


You are missing the point of REST...the fact that there is one URI which responds to different verbs from the uniform interface (GET, POST, PUT, DELETE), makes it restful. So Backbone is actually being RESTful, while you're not.


The default backbone sync method works exactly how you want it to already by appending the models id to the url when performing an update.

If you need to customize how data is sent to your server I've found the best thing is to create your own backbone sync. Here is an example of how I do it to wrap my create and update requests in a root json object: https://github.com/codebrew/rails3-backbone-coffeescript/blob/master/app/coffeescripts/lib/mongo_model.coffee

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜