How to get model params on an adapter

I’m trying to fetch data from the following URL structure:

${ENV.APP.API_HOST}/api/v1/customers/:customer_orgCode/sites/

I’m using an adapter to shape the request with buildURL with the following files:

// router.js
this.route('sites', { path: 'customers/:customer_orgCode/sites' }, function() {
    this.route('show', { path: ':site_id' });
});

// adapters/site.js
export default ApplicationAdapter.extend({
  buildURL (modelName, id, snapshot, requestType, query) {
    // return `${ENV.APP.API_HOST}/api/v1/customers/${snapshot???}/sites/`;
    return `${ENV.APP.API_HOST}/api/v1/customers/239/sites/`;
  }
}

// routes/sites/index.js
export default Ember.Route.extend({
  model: function() {
    let superQuery = this._super(...arguments),
        org = superQuery.customer_orgCode;
    this.store.findAll('site', org);
  }
});

The ‘site’ model is located at:

// models/site.js

I’m able to get the customer_orgCode on the route, but unable to pull it into the adapter. I’ve noted that the model isn’t being populated in the Ember inspector, but the sites data is present when I make the request. Does anyone know how I can dynamically populate the buildURL with the customer_orgCode from the params on the router? And then specify sites/index to use the ‘site’ model?

You should use query instead of findAll. query is specifically for returning all records based on some parameters.

this.store.query('site', {org: org});

The parameters are then available in the query variable in the buildURL function

1 Like

Perfect, thanks @Subtletree. Below is the final implementation for anyone who’s interested. For the model, all I needed to add to the model() function was a return statement and it populated the model on sites.index from the route.

// router.js
this.route('sites', { path: 'customers/:customer_orgCode/sites' }, function() {
    this.route('show', { path: ':site_id' });
});

// adapters/site.js
export default ApplicationAdapter.extend({
  buildURL (modelName, id, snapshot, requestType, query) {
    let org = query.org;
    return `${ENV.APP.API_HOST}/api/v1/customers/${org}/sites/`;
  }
});

// routes/sites/index.js
export default Ember.Route.extend({
  model: function() {
    let superQuery = this._super(...arguments),
        org = superQuery.customer_orgCode;
    return this.store.query('site', {org:org});
  }
});
1 Like