Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you access RESTAdapter's host and namespace inside a route or controller?

I have a few custom AJAX requests that I use inside of some controllers and routes, for example:

 var loginRoute = Ember.Route.extend({

   actions: {

     submitLogin: function(user, pass) {

       var data = { username: user, password: pass };

       Ember.$.post('http://192.168.2.10/api/v1/login', data).then();
     }

   }
 });

This works fine, but while developing I may have a different IP (e.g. changing routers) and I'd like to be able to access the URL(host + namespace) I defined when I extended the RESTAdapter so that I only have to change the host and/or namespace once, instead of every place where I do a custom ajax request.

 App.ApplicationAdapter = DS.RESTAdapter.extend({
   host: 'http://192.168.2.10',
   namespace: 'api/v1'
 });
like image 365
Joe B Avatar asked Dec 04 '13 23:12

Joe B


1 Answers

turns out you can access the Adapter from the store via this.store.adapterFor('application')

The new submitLogin method could look like this:

 submitLogin: function(user, pass) {

   var data = { username: user, password: pass },
       host = this.store.adapterFor('application').get('host'),
       namespace = this.store.adapterFor('application').namespace,
       postUrl = [ host, namespace, 'login' ].join('/'); // http://192.168.2.10/api/v1/login

   Ember.$.post(postUrl, data).then();
 }
like image 140
Joe B Avatar answered Oct 11 '22 19:10

Joe B