Search code examples
ember.jsember-dataember-cli

Ember Subdirectory Models vs Adapter Change?


I noticed that it's possible to create subdirectory models with ember cli. So I can do

ember g model log/type1-log

This creates a log directory with the type1-log model inside it. The reason I attempted this was that of my API structure.

/api/v1/logs/
/api/v1/logs/type1-log
/api/v1/logs/type2-log
/api/v1/logs/type3-log

Initially, I was doing ember g model type1-log, but that model was doing api requests to /api/v1/type1-log. I wanted it to be doing api/v1/logs/type1-log.

Are sub directory models supported by Ember, or should I actually be updating my Adapter to modify the URL?

Requesting logs is not the only thing my api will do in the future so I do not want to set a namespace to /api/v1/logs in the adapter.


Solution

  • You can customize end point customization using the adapter. so in your case I would say, You can create model specific adapter and use namespace property to customize it.

    ember g adapter type1-log
    

    and

    import DS from 'ember-data';
    export default DS.JSONAPIAdapter.extend({
      namespace: 'api/v1/logs'
    });
    

    From ember guides,

    If you have one model that has exceptional rules for communicating with its backend than the others you can create a Model specific Adapter by running the command ember generate adapter adapter-name. For example, running ember generate adapter post will create the app/adapters/post.js file.

    Reference:
    https://guides.emberjs.com/v2.14.0/models/customizing-adapters/ https://emberjs.com/api/ember-data/2.14/classes/DS.JSONAPIAdapter