我正在使用EmberJS 1.13。我想在用户登录时显示用户名(在application.hbs上)。
templats / application.hbs
<div id="main-frame" class="container-fluid">
<div id="page-header">
<img src="/assets/logo-main.png">
{{#if isLoggedIn}}
<span>{{currentUser.displayName}}</span>
{{else}}
<a id="loginButton" class="text-button" data-toggle="modal" data-target="#loginWindow">Войти</a>
{{/if}}
</div>
<div id="content-frame" class="container">
<ul class="nav nav-pills">
<li>{{#link-to 'builder'}}Конструктор{{/link-to}}</li>
<li><a href="#">Мой бар</a></li>
<li><a href="#">Админ-панель</a></li>
</ul>
<hr>
<hr>
{{outlet}}
</div>
</div>
{{login-window authSuccess="authSuccess"}}
{{signup-window}}
路由/ application.js中
import Ember from 'ember';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';
export default Ember.Route.extend(ApplicationRouteMixin, {
currentUser: Ember.inject.service('current-user'),
isLoggedIn: Ember.computed.bool('currentUser.isAuthenticated'),
actions: {
authSuccess: function(userInfo) {
this.get('currentUser').setUser(userInfo);
}
}
});
服务/电流user.js的
import Ember from 'ember';
export default Ember.Service.extend({
session: Ember.inject.service("session"),
username: "",
password: "",
displayName: "",
accessLevel: -1,
isLoggedIn: false,
isAuthenticated: function () {
return this.get("isLoggedIn") && this.get('session').isAuthenticated;
}.property('isLoggedIn'),
setUser: function(userInfo) {
this.set("username", userInfo.username);
this.set("password", userInfo.password);
this.set("displayName", userInfo.displayName);
this.set("accessLevel", userInfo.accessLevel);
this.set("isLoggedIn", true);
}
});
我有下一个行为: currentUser.setUser() 被调用, currentUser.isLoggedIn 已设置,但< strong> currentUser.isAuthenticated 不会重新计算html中的用户名。我做错了什么?
更新:
我还尝试了不同的方法实现isLoggedIn
属性:
isAuthenticatedObserver: Ember.on('init', Ember.observer('isLoggedIn', function () {
this.set('isAuthenticated', this.get("isLoggedIn") && this.get('session').isAuthenticated);
}))
////
isLoggedIn: function() {
return this.get('currentUser').isAuthenticated;
}.observes('currentUser.isAuthenticated')
////
isLoggedIn: function() {
return this.get('currentUser').isAuthenticated;
}.property('currentUser.isAuthenticated')
但没有改变。