0

I have a Host model on ember-data:

App.Host = DS.Model.extend
  name: DS.attr 'string'

And I using the DS.RESTAdapter.

EmberJS calls /hosts to get hosts which is normal.

My problem is the url to get hosts is not /hosts but /nagios/hosts.

How can I specify this url only for Host model?

I already tried those answers:

Without any success.

Currently using:

Ember      : 1.10.0
Ember Data : 1.0.0-beta.15
jQuery     : 2.0.3

Thanks for help.

Community
  • 1
  • 1
Soullivaneuh
  • 3,553
  • 3
  • 37
  • 71

3 Answers3

3

Add an adapter (ember generate adapter hosts) just for the hosts model and add the namespace property:

App.HostsAdapter = DS.RESTAdapter.extend({
  namespace: 'nagios'
});
enspandi
  • 663
  • 4
  • 6
  • Where should I put this by convention? On my model file? – Soullivaneuh Mar 23 '15 at 09:53
  • If you are using Ember CLI you could use the ember generator like: ember generate adapter hosts and it will generate a folder called "hosts.js" in "adapters". So the full path is .../app/adapters/hosts.js – enspandi Mar 23 '15 at 09:54
  • I'm not using Ember CLI. I tested your solution, did nothing. What could I did wrong? – Soullivaneuh Mar 23 '15 at 09:55
  • You could try to name it: App.HostAdapter instead of App.HostsAdapter, but make sure that this code is somewhere included or create an own file like hosts-adapter.js and include it. – enspandi Mar 23 '15 at 09:57
  • Here are the docs for further reading: http://emberjs.com/guides/models/the-rest-adapter/ It is definitely possible to create an adapter for a single model by just following the naming conventions. – enspandi Mar 23 '15 at 09:59
  • Works with App.HostAdapter indeed! But the namespace option override completely my 'api' main namespace. Is there a where to retrieve and concat it with the host namespace? Thanks. – Soullivaneuh Mar 23 '15 at 10:00
  • Found it. Will extend your answer. Thanks a lot! – Soullivaneuh Mar 23 '15 at 10:02
1

Finally found the correct solution.

For begin by the context, I have an initial RESTAdapter config:

App.ApplicationAdapter = DS.RESTAdapter.extend
  host: 'http://localhost:8000/api'

To add the /nagios url part, just create a file named adapters/host.coffee (or .js) like this:

App.HostAdapter = App.ApplicationAdapter.extend
  namespace: 'nagios'

Please not App.ApplicationAdapter extension instead of DS.RESTAdapter in order to keep the initial configuration.

And voila! The called url is now http://localhost:8000/api/nagios/hosts.

Thanks a lot to EntspAndi for the very useful help!

Community
  • 1
  • 1
Soullivaneuh
  • 3,553
  • 3
  • 37
  • 71
0

namespace for setting a path in same directory

App.HostsAdapter = DS.RESTAdapter.extend({namespace: 'nagios' });

Or

host generally is used for changing the directory

App.HostsAdapter = DS.RESTAdapter.extend({ host: 'nagios' });

In case you want to add suffix to the file name do it this way (place this inside extend()).

suffix: '.json',

      pathForType: function(type) {
        return this._super(type) + this.get('suffix');
      }
sidharth
  • 3
  • 6