在承诺中设置属性后,Ember控制器不刷新模板

时间:2014-02-24 23:46:21

标签: javascript ember.js ember-data ember-controllers

我可能正在做这个可怕的错误,但是这里是:我从服务器获取用户,一旦解决了,我试图将用户的名称呈现给页面。登录表单实际上是一个基于此的弹出窗口:http://www.sociallipstick.com/?p=86

不幸的是,当我将userLoggedIn设置为true时,模板根本不会改变。我做错了吗?

App.UserController = Ember.Controller.extend({
    userLoggedIn: false,
    actions: {
        displayLoginForm: function () {
            //displays a login form
        },
        recieveLogin: function (authResult) {
            //hides login form
            var userPromise = this.store.find('user', authResult); // successfully gets user from server (as far as I can tell)
            var self = this;
            userPromise.then(function (user) {
                self.set('model', user);
                self.set('userLoggedIn', true);
            });
        }
    }
});
<li class="navbar-form">
    {{#if userLoggedIn}}
        <a href="#" class="dropdown-toggle" data-toggle="dropdown"><b class="caret"></b></a>
        <ul class="dropdown-menu">
            <li><a href="#">My quizzes</a></li>
            <li><a href="#">My scores</a></li>
            <li><a href="#">Settings</a></li>
            <li class="divider"></li>
            <li><a href="#">Logout</a></li>
        </ul>
    {{else}}
        <button class="btn btn-default" {{action 'displayLoginForm'}}>Login</button>
    {{/if}}
</li>

1 个答案:

答案 0 :(得分:0)

这个错误实际上是由一个完全不同的问题引起的。登录脚本的回调使用[hacky]行从Ember外部的弹出窗口获取控制器:

window.opener.App.__container__.lookup('controller:User').send('recieveLogin', 'USERIDHERE');

不幸的是,这实际上得到了一个不同的控制器实例......

所以,我修改了控制器:

App.UserController = Ember.Controller.extend({
    userLoggedIn: false,
    actions: {
        displayLoginForm: function () {
            //displays a login form
            /******** THIS IS THE IMPORTANT LINE ********/
            App.UserController.callback = this;
            /******** THAT WAS THE IMPORTANT LINE ********/
        },
        recieveLogin: function (authResult) {
            //hides login form
            var userPromise = this.store.find('user', authResult); // successfully gets user from server (as far as I can tell)
            var self = this;
            userPromise.then(function (user) {
                self.set('model', user);
                self.set('userLoggedIn', true);
            });
        }
    }
});

然后我在页面上使用了稍微不那么粗俗的一行:

window.opener.App.UserController.callback.send('recieveLogin', 'USERIDHERE');