0
我正在使用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);
}
});
我旁邊的行爲:CUR rentUser.setUser()叫,currentUser.isLoggedIn設置好的,但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')
但什麼都沒有改變。
很難說明它爲什麼不起作用。可能會將兩個屬性都添加到computedProperty中。 property('isLoggedIn','session.isAuthenticated'), –
是否調用authSuccess'動作? –
@Daniel Kmak authSuccess調用。但是currentUser.isAuthenticated從來沒有打過電話。 – Crabar