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

拈花ヽ惹草 提交于 2019-12-18 19:06:00

问题


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'
 });

回答1:


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();
 }


来源:https://stackoverflow.com/questions/20388235/how-do-you-access-restadapters-host-and-namespace-inside-a-route-or-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!