Ember.js current_user - 从控制器访问全局变量 [英] Ember.js current_user - accessing global variable from controller

查看:153
本文介绍了Ember.js current_user - 从控制器访问全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对一个看似简单的垃圾问题感到困惑。我正在使用 active_model_serializers元数据序列化从rails控制器序列化我的rails current_user方法,然后使用这个人的临时解决方案

  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信息,例如:

  {{App.metaData.current_user.email}} 

,该电子邮件属性显示在模板中。当我尝试从一个ember控制器中访问该全局变量时,问题出现。这似乎是非常容易的,但我似乎无法从任何一个ember控制器中访问current_user。我试过:

  currentUserBinding:'App.metaData.current_user',

currentUser:function ){
return App.metaData.current_user;
} .property(),

Ember.get('App.metaData.current_user')

等几种方法,但似乎无法访问它。这是奇怪的,我可以从控制台和手柄访问current_user直接路径,但不是从一个ember控制器。我一定要错过一些明显的东西。



感谢任何帮助!

解决方案

在我们的应用程序中它与@MilkyWayJoe建议的一样,但我认为你的方法真的很有趣。传递current_user通过JSON是一个非常优雅的方法。



我没有尝试太多,但从我可以在你的例子中可以看出,问题与...无关串行器。这是计算属性的一个问题 - 您需要指定它取决于 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应该重新计算currentUser属性。 metaData.currentUser 更改。否则将运行fx一次(在您的ajax返回之前)并缓存响应。


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

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}}

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

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.

Would appreciate any help!

解决方案

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.

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆