我应该如何通过用户的登录状态,以我的Ember.js应用程序? [英] How should I pass user's login status to my Ember.js application?
问题描述
我有有很多这是只有在用户签订的可用选项的交互式应用程序灰烬。例如,有可能是发表的文章列表,然后还有另一个链接的我的文章的,这是与用户相关。
I have an interactive Ember app which has a lot of options which are available only to signed in users. For example there might be a list of posts, and then there's another link to my posts, which are relevant to the user.
有浮现在我的脑海两个问题:
There are two issues that come to my mind:
- 我该如何告诉应用程序用户是否登录,也许他的数据?
- 如何隐藏特定的功能和项目根据他的登录状态?
是否有处理这个的最佳做法?我想登录过程本身不会那么复杂,但如果LOGGED_IN? do_x其他do_y
是一个巨大的未知数,我大气压。我应该在哪里存储了全球用户状态?
Are there any best practices for approaching this? I guess the login process itself won't be that complicated, but the if logged_in? do_x else do_y
is a big unknown for me atm. Where should I store the global user state?
推荐答案
如果您使用的是路由器灰烬,那么我的建议是建筑师这样一个解决方案。
If you are using the ember router, then my suggestion would be to architect a solution like this
的LoginController
App.LoginController = Ember.Controller.extend({
login: function(params){ /* Your logic here */ },
logout: function(params){ /* Your logic here */},
user_data_hash: { first_name: "The", last_name: "Hobbit"},
is_logged_in: (function() {
/* do some logic and return true or false */
}).property('some_item_on_user_data_hash')
just_logged_in: (function() {
/* do some logic and return true or false */
}).property('some_item_on_user_data_hash')
然后在你的路由器之前,你可以导航到一个受保护的路径,你的的LoginController
对象进行检查。这个例子是从拿这个答案。
Then in your router before you allow navigation to a protected route, you check with the LoginController
object. This example is take from this answer.
root: Ember.Route.extend({
index: Ember.Route.extend({
enter: function(router) {
var logged_in = router.get('loginController.is_logged_in'); /*Or in older ember builds `router.getPath('loginController.is_logged_in');`*/
var just_logged_in = router.get('loginController.just_logged_in'); /*Or in older ember builds `router.getPath('loginController.just_logged_in');`*/
Ember.run.next(function() {
if (logged_in && just_logged_in) {
router.transitionTo('loggedIn');
} else if (!logged_in) {
router.transitionTo('loggedOut');
}
});
}
}),
loggedIn: Ember.Route.extend({
// ...
}),
loggedOut: Ember.Route.extend({
// ...
})
})
这篇关于我应该如何通过用户的登录状态,以我的Ember.js应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!