我正在编写一个混合应用程序,其中包含一些服务器处理和用Ember实现的主UI部分。Ember:共同财产的地方?
所有与认证有关的功能都是基于服务器的,所以当加载页面时我已经知道(基于cookie)如果用户被认证或不认证。
总之 - 在客户端,我有一个userId
cookie,如果它被设置,那么用户被认证。
现在我需要将这些数据提供给所有模板。
我解决它的应用程序模板(在CoffeeScript中的所有代码,但没有什么特别有语言):
路线
ApplicationRoute = Ember.Route.extend
setupController: (controller) ->
userId = jQuery.cookie 'userId'
if userId == 'undefined'
userId = null
else
userId = parseInt userId, 10
controller.set 'userId', userId
控制器
ApplicationController = Ember.Controller.extend
userId: null
最后,模板
<strong>
{{#if userId}}
userId: {{userId}}
{{else}}
No user
{{/if}}
</strong>
这从应用程序模板起作用,但如果我将它移动到index
模板,它总是会显示'没有用户'(我期望控制器有一些原型链遍历)。
我试着移动它boud帮手 - 不工作为好,助手不会被调用都:
Ember.Handlebars.registerBoundHelper 'userId', ->
userId = jQuery.cookie 'userId'
if userId == 'undefined'
userId = null
else
userId = parseInt userId, 10
userId
你可能想看看[这个问题](http:// stackoverflow。com/questions/16070390/ember-js-current-user-access-global-variable-from-controller)它有点类似于 – MilkyWayJoe 2013-05-08 13:57:12
类似但不完全。但它看起来像我可以通过将数据附加到全局应用程序对象来解决它。 – Guard 2013-05-08 18:35:37