samedi 7 novembre 2015

Computed property doesn't work in a service

I'm using EmberJS 1.13. I want to show username (on application.hbs) when an user is logged in.

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}}

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

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

I have next behavior: currentUser.setUser() called, currentUser.isLoggedIn setted, but currentUser.isAuthenticated doesn't recalculates and no username in html. What I do wrong?




Aucun commentaire:

Enregistrer un commentaire