How to handle singular resources with RESTAdapter

2019-04-19 03:05发布

问题:

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?

回答1:

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"
  })
});


回答2:

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.



回答3:

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).



回答4:

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', '');
    }
});