2
votes

I'd like somebody to tell me how they've approached the issue using custom member or collection actions for a REST resource, e.g. PUT /users/:id/confirm or PUT /kittens/pet_all

This is obviously a complex task. To properly implement it would be difficult because outside of resourceful routes, there is no assumed contract for the server's request/response type or structure. It sucks, though, when hypothetically all I want to do is pet_all my kittens, regardless of the outcome and must resort to hacking together an ajax request to do so.

Please, think of the kittens...

1

1 Answers

-1
votes

One way I've done this in the past was to override the findQuery method in my adapter and pass a hash to the find method. This will skip the buildURL method altogether.

For example, if you wanted to fetch a collection of confirmations nested in a user resource:

App.UserRoute = Em.Route.extend({
  afterModel: function(model) {
    promise = this.get('store').find('confirmation', userId: model.get('id'))
    this.controllerFor('confirmations').set('model', promise)
  }
});

App.ConfirmationAdapter = DS.RESTAdapter.extend({
  findQuery: function(store, type, query) {
    confirmationsUrl = this.host + '/users/' + query.userId + '/confirmations'
    delete query.userId
    return this.ajax(confirmationsUrl, 'GET', { data: query });
  }
})

This should get you started in figuring out how to handle updates.