Macchiatow
Macchiatow

Reputation: 607

Ember-data with complex url

Killed few days trying to resolve this issue.

Normally I use Embers conversion to get a model from store:

OlaMagic.DashboardIndexRoute = Ember.Route.extend({
    model: function(params) {
        return this.store.findAll('number');


    }

});

This would resolve to GET => DS: RESTAdapter#ajax GET to http://localhost:8080/api/numbers

But I CANNOT find a ways to execute a request against this URL: http://localhost:8080/api/profiles/:profile_id/workspaces

BTW http://localhost:8080/api/profiles/:profile_id does not return a key to iterate over workspaces. The only way to get all workspaces is via direct url.

Upvotes: 1

Views: 555

Answers (1)

Kit Sunde
Kit Sunde

Reputation: 37065

Well this isn't a normal way for the REST adapter to build URLs. At minimum you would need to make a special adapter for the workspaces model then you'll need to override urlForQuery (if it's request specific) and specify and move some of the logic that's inside of _buildURL into that.

adapters/workspaces.js

import DS from 'ember-data';

export default DS.RESTAdapter.extend({
  urlForQuery: function(query, modelName){
    var url = ['api','profiles', query.profile, 'workspaces'];
    delete query.profile;
    var host = this.get('host');
    var prefix = this.urlPrefix();

    url = url.join('/');
    if (!host && url && url.charAt(0) !== '/') {
      url = '/' + url;
    }

    return url;
  }
});

Then you do something like this:

OlaMagic.DashboardIndexRoute = Ember.Route.extend({
  model: function(params) {
    return this.store.query('workspace', {profile: 1});
  }
});

This will generate a request to /api/profiles/1/workspaces.

If you're only ever requesting workspaces on the current user then it could probably be enough to just overriding init on the adapter for workspaces and setting a deeper namespace on it on authentication.

Upvotes: 2

Related Questions