Ember.js current_user - 从控制器访问全局变量 [英] Ember.js current_user - accessing global variable from controller
问题描述
我对一个看似简单的余烬问题感到困惑.我正在使用 active_model_serializers 元数据序列化 从 rails 控制器序列化我的 rails current_user 方法,然后使用 这个人的临时解决方案.
I am baffled by a seemingly simple ember question. I am using active_model_serializers meta data serialization to serialize my rails current_user method from a rails controller, then extracting and setting that current_user meta json to a global ember variable using this guy's temporary solution.
App.CustomRESTSerializer = DS.RESTSerializer.extend({
extractMeta: function(loader, type, json) {
var meta;
meta = json[this.configOption(type, 'meta')];
if (!meta) { return; }
Ember.set('App.metaData', meta);
this._super(loader, type, json);
}
});
到目前为止一切都很好.现在我可以通过输入 App.metaData.current_user 从控制台访问 current_user 信息,也可以通过调用特定属性从把手访问 current_user 信息,即:
All good up until this point. Now I can access the current_user information from the console by typing in App.metaData.current_user and also from handlebars by calling a specific attribute, i.e.:
{{App.metaData.current_user.email}}
并且该电子邮件属性显示在模板中.当我尝试从 ember 控制器中访问该全局变量时,问题就出现了.这看起来非常简单,但我似乎无法从任何 ember 控制器中访问 current_user.我试过了:
and that email attribute shows up in the template. The problem comes when I am trying to access that global variable from within an ember controller. It seems so ridiculously easy, but I can't seem to access current_user from within any ember controller. I have tried:
currentUserBinding: 'App.metaData.current_user',
currentUser: function() {
return App.metaData.current_user;
}.property(),
Ember.get('App.metaData.current_user')
和其他一些方法,但似乎无法访问它.奇怪的是,我可以通过直接路径从控制台和把手中如此轻松地访问 current_user,但不能从 ember 控制器中访问.我一定遗漏了一些明显的东西.
and a few other methods, but can't seem to access it. It's bizarre that I can access current_user so easily from within the console and handlebars with a direct path, but not from within an ember controller. I must be missing something obvious.
非常感谢您的帮助!
推荐答案
在我们的应用程序中,我们按照@MilkyWayJoe 的建议进行操作,但我认为您的方法非常有趣.通过 JSON 传递 current_user 是一种非常优雅的方法.
In our app we do it same as @MilkyWayJoe suggested, but I think your approach is really interesting. Passing current_user thru JSON is a really elegant approach.
我没有做过太多实验,但是从我在您的示例中可以看出,问题与序列化程序无关.这是计算属性的问题 - 您需要指定它依赖于 App.metaData.current_user
.
I've not experimented too much, but from what I can tell in your example the problem is nothing to do with the serializer. It's a problem with the computed property - you need to specify that it depends on App.metaData.current_user
.
App.ApplicationController = Ember.Controller.extend({
currentUser: function() {
return Ember.get('App.metaData.current_user')
}.property('App.metaData.current_user')
});
这告诉 ember 它应该在 App.metaData.currentUser
更改时重新计算 currentUser 属性.否则它将运行一次 fx(在您的 ajax 返回之前)并缓存响应.
This tells ember that it should re-calculate the currentUser property whenever App.metaData.currentUser
changes. Otherwise it will run the fx once (before your ajax returns) and cache the response.
这篇关于Ember.js current_user - 从控制器访问全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!