Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle singular resources with RESTAdapter

How are singular resources handled in ember-data? Say I have the following RESTful routes:

GET /cart
POST /cart
UPDATE /cart
DELETE /cart

ember-data expects find() to return an array, plus it automatically tries to pluralize any url I pass to my model. What is the best way to handle this situation?

like image 907
Nick Colgan Avatar asked Sep 22 '12 01:09

Nick Colgan


4 Answers

There are a number of things you can do here.

The RESTAdapter calls pluralize, which either adds an "s" to the end of the name, or looks up the name in the plurals hash if it exists. Assuming your DS.Model is App.Cart.

https://github.com/emberjs/data/blob/master/packages/ember-data/lib/adapters/rest_adapter.js#L209

DS.RESTAdapter.create({
  plurals: {
    cart: 'cart'
  }
});

If your URL scheme is very different and requires some further logic, you can actually override the buildURL function.

https://github.com/emberjs/data/blob/master/packages/ember-data/lib/adapters/rest_adapter.js#L288

DS.RestAdapter.create({
  buildURL: function() {
    return "/always_this"
  })
});
like image 93
Ryan Avatar answered Nov 10 '22 02:11

Ryan


So, I found this pull request on github. It's 8 months old, so won't work due to added complexity since then, but I've implemented the workaround suggested like so:

App.store = DS.Store.create({
  revision: 4,
  adapter: DS.RESTAdapter.create({
    plurals: {
      'cart': 'cart'
    }
  })
});

App.Cart.reopenClass({
  find: function () {
    this._super("singleton");
  }
});

On my server (I'm using rails), I have to add the following to my routes:

get "cart/:ignored" => "carts#show"

Then I have to add the following to CartSerializer (using active_model_serializers gem):

attributes :id
def id
  "singleton"
end

This is necessary, because, apparently, if the id in the json response doesn't match the id requested from find() (singleton in this case), then ember won't load the data into the model.

Now, this obviously isn't the ideal solution, but until ember-data adds support for it, it seems like the least painful way to go.

By the way, I filed an issue to add support.

like image 3
Nick Colgan Avatar answered Nov 10 '22 03:11

Nick Colgan


Here is how I got this working in Ember 1.9. First I read this section of the guide. At the very bottom it explains how to override an adapter for just one model.

App.CartAdapter = App.ApplicationAdapter.extend {
  pathForType: ->
    'cart'
}

The pathForType function is where the pluralization happens (at least in the RESTAdapter which I am using) so none of the other functionality of the adapter gets affected (like host, or namespace).

like image 2
snocorp Avatar answered Nov 10 '22 03:11

snocorp


Just to share a more complete solution which is working for me - extend your app's ApplicationRouter (which itself extends DS.RESTAdapter).

App.CartAdapter = App.ApplicationAdapter.extend({
    pathForType: function(type) {
        return 'cart';
    }
});

Next, define your resource in App.Router.map:

this.resource('cart');

Finally, pass an empty string as the id in your route. This is what allows the URL to be generated without an id.

App.CartRoute = Ember.Route.extend({
    model : function(params) {
        return this.store.find('cart', '');
    }
});
like image 1
Michael Edgar Avatar answered Nov 10 '22 04:11

Michael Edgar