计算属性在服务中不起作用

克拉巴

我正在使用EmberJS 1.13。我想在用户登录时显示用户名(在application.hbs上)。

模板/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);
        }
    }
});

服务/current-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已设置,但是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')

但是什么都没有改变。

克拉巴

我在余烬论坛上得到了答复因此,模板中使用的所有属性都必须在控制器中,但不能在路径中。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章