简体   繁体   中英

Emberjs + Rails: /api or respond_to?

I'm going to use Emberjs to an existing Rails app and itt have already some scaffold resources.

In this guide I've read that is better to create a path like: "/api/posts" to handle request for emberjs. But I know that Rails responds to JSON if I pass ".json" to my url: eg; /posts.json

So, should I create a /api or I could use my default controller to handle JSON? which is the better choise? If I use default controller i'll use something like

 def index
    @posts = Post.find(:all)

    respond_to do |format|
      format.html
      format.json { render :json => @posts.to_json }
    end
  end

One possible solution might be to hook into the buildURL function of your RESTAdapter and adding the .json suffix yourself. This could look something like this:

App.Adapter = DS.RESTAdapter.extend({
  buildURL: function(record, suffix) {
    var url = this._super(record, suffix);
    return url + ".json";
  }
})

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM