我想在用户登录后更新 View 。从服务器获取他的个人资料图片、姓名等,并使用该信息刷新 View 。而且我不想改变我所在的路线。
我处理登录的方式是使用 bool 变量isLoggedIn
。用户登录后,我将其值更改为 true。我有一个绑定(bind)监听该变量的更改,然后使用用户信息执行对实际路线模型的更新。
App.TripsRoute = Ember.Route.extend
isLoggedInBinding: 'App.Session.isLoggedIn'
isLoggedInChanged: (->
@model()
).observes('isLoggedIn')
model: ->
App.Trip.find()
所以问题是:有没有办法从路由的自定义方法内部访问动态参数来更新相关模型?
注意:我不想在路由的模型 Hook 内执行此操作,因为它仅在通过 URL 输入时执行。
最佳答案
您可以在App
中存储对当前用户的引用,并访问该引用以确定用户是否已登录,并使用详细信息更新当前 View 。
存储currentUser
:
App = Ember.Application.create({
currentUser: null
});
然后可以使用 App.get('currentUser')
或 App.set('currentUser', newUser)
在应用中的任何位置获取/设置该值。
在您的模板中,您可以检查用户是否登录,并在登录后自动显示其信息:
{{#if App.currentUser }}
<img {{bindAttr src='App.currentUser.avatarURL'}} /> </br>
Logged in as: {{App.currentUser.name}}
{{else}}
Not logged in.
{{/if}}
使用当前用户的用户对象更新 App.currentUser
后,Ember 的数据绑定(bind)将自动使用个人资料信息更新您的 View 。
关于javascript - 访问动态参数 EmberJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16798277/