ember.js - Adding headers after RESTAdapter initialization -
i trying add authorization
header adapter's request after adapter has been initialized , used. can add headers in static way @ time create applicationadapter
, can't seem use headers in subsequent rest calls. trying this:
var auth= "basic " + hash; app.applicationadapter.reopen({ headers: { authorization: auth } });
i have debugged restadapter
in ajax
method, , test adapter.headers
undefined
.
the accepted answer doesn't address fact recommended approach not working in ember-data. recommended since:
https://github.com/emberjs/data/blob/master/packages/ember-data/lib/adapters/rest_adapter.js#l88
https://github.com/emberjs/data/blob/master/packages/ember-data/lib/adapters/rest_adapter.js#l162 , other places in file.
further, issue op brings of undefined happens here: https://github.com/emberjs/data/blob/master/packages/ember-data/lib/adapters/rest_adapter.js#l619
so, following not work:
app.applicationadapter.reopen({ headers: {token: 'reopen_token (no work)' } });
i've tried point out issue got closed within hour: https://github.com/emberjs/data/issues/1820
hopefully core decide either fix or remove comments. but, yes, seems have hijack jquery ajax setup, ember.$.ajaxprefilter, or override ajax
on adapter yourself.
edit: after getting more feedback ember devs, looks core of issue trying reopen instance created. using computered property when it's defined (so update desired) seems advised approach. hope helps (there's merged pull request makes more obvious in comments of referenced file:https://github.com/emberjs/data/pull/1818/files#diff-1d7f5a5b77898df15de501c3c38d4829r108 )
edit 2: got working in app here's code in case else gets stuck:
//app.js app.applicationadapter = ds.activemodeladapter.extend({ namespace: 'api/v1', headers: function() { return { token: this.get('app.authtoken') || localstorage.getitem('token') }; }.property("app.authtoken") }); //login-controller.js (only action shown..assume `data` has user/pass) actions: { login: function() { $.post('/token/', data).done(function(user) { app.set('authtoken', user.token); //above trigger adapters's header computed property update // transition previous attempted route var attemptedtransition = self.get('attemptedtransition'); if(attemptedtransition) { attemptedtransition.retry(); } else { self.transitiontoroute('yourapproute'); } }) .fail(function(response) { //fail handling omitted });
Comments
Post a Comment