在用户注册到Ember数据请求头后,我试图设置从响应中接收到的令牌。
这是我的application.js代码
import DS from 'ember-data';
export default DS.RESTAdapter.extend({
namespace: 'merchant',
host: 'http://192.168.1.173:3000',
headers: function() {
return {
"token":localStorage.token,
};
}.property("localStorage.token","token")
});在收到来自注册用户API调用的成功响应后,我正在尝试设置标题。
var register=this.store.createRecord('registermerchant',data);
register.save().then(function(response){
console.log(response.success);
if(response.get('success')){
self.set('token',response.get('token'));
self.transitionToRoute('merchanthome')
}和
tokenChanged: function() {
localStorage.token=this.get('token');
console.log(localStorage.token);
}.observes('token'),我能够看到更新的localStorage.token值,但是由于某种原因,这个值没有被设置为reqest报头令牌键。
令牌没有更新到标头,只有在执行页面刷新之后才会将新更新的令牌发送到服务器。
任何关于我可能做错了什么的想法,任何提示都将不胜感激。谢谢
发布于 2015-08-07 20:09:08
localStorage不是一个可观察的Ember对象,因此您的property("localStorage.token")只需要计算一次。相反,每次重新评估该属性时,都会使其不稳定:
headers: function() {
return {
"token":localStorage.token,
};
}.property().volatile()https://stackoverflow.com/questions/31885383
复制相似问题