Emberjs不能委托集

时间:2014-03-07 15:25:46

标签: ember.js

当我在最后构建我的应用程序时,我想测试我的所有功能,但主要功能已停止工作......

我的登录表单似乎无法从表单中获取用户信息。

我的登录模板:

<script type="text/x-handlebars" data-template-name="login">
    <form {{action "login" on=submit}}>
        <div>
        <div>
            <label>Domain</label>
        </div>
        <div>
            {{input type="text" value=domain placeholder="Domain"}}

        </div>
    </div>

    <div>
        <div>
            <label>Username</label>
        </div>
        <div>
            {{input value=username type="text" placeholder="Username"}}
        </div>
    </div>

    <div>
        <div>
            <label>Password</label>
        </div>
        <div>
            {{input value=password type="password" placeholder="Password"}}
        </div>
    </div>
    <button type="submit" {{bind-attr disabled="isProcessing"}}>Log in</button>
    </form>
</script>

我的loginController:

App.LoginController = Ember.ObjectController.extend({
    actions: {
        login: function() {
            var data = this.getProperties("domain", "username", "password");

            console.log(data);
        }
    }
});

我的操作“登录”已被触发,但我有那些我无法解释的错误:

Assertion failed: Cannot delegate set('domain', g) to the 'content' property of object proxy <App.LoginController:ember330>: its 'content' is undefined.

Uncaught Error: Property set failed: object in path "domain" could not be found or was destroyed.

它起初工作,我必须说我不知道​​那是什么问题。

[edit]这是我的LoginController&amp;我的地图:

App.Router.map(function() {
    this.route('login', { path: '/' });
    this.route('home');
});

App.LoginController = Ember.ObjectController.extend({
    loginFailed: false,
    isProcessing: false,
    isSlowConnection: false,
    timeout: null,

    actions: {
        isSession: function(transition) {
            var session = this;
            Ember.$
                .get(host + '/session', function(data) {
                    console.log('DEBUG: Session OK');
                    var log = data.user;
                    session.store.push('login', log);
                })
                .fail(function() {
                    console.log('DEBUG: Session FAIL');
                    transition.abort();
                    session.transitionToRoute('login');
                });
        },
        login: function() {
            this.setProperties({
                loginFailed: false,
                isProcessing: true
            });
            this.set("timeout", setTimeout(this.slowConnection.bind(this), 1));
            var data = this.getProperties("domain", "username", "password");

            console.log(data);
            this.getLoginInfo(data);
        }
    },
    slowConnection: function() {
        this.set('isSlowConnection', true);
    },
    reset: function() {
        clearTimeout(this.get("timeout"));
        this.setProperties({
            loginFailed: false,
            isSlowConnection: false,
            isProcessing: false
        });
    },
    getLoginInfo: function(data) {
        var login = this;
        Ember.$
        .post(host + '/login', data, function(data) {
            console.log('DEBUG: Login OK');
            login.reset();
            var test = data.session.user;
            login.store.push('login', test);
            login.transitionToRoute('home');
        })
        .fail(function() {
            login.reset();
            login.set('loginFailed', true);
            console.log('DEBUG: Login Failed')
        });
    }
});

我在其他页面的每个beforeModel中使用isSession函数来验证会话是否仍然可用,如果是,则将登录数据推回到商店以再次保存。

2 个答案:

答案 0 :(得分:14)

您为登录控制器扩展了Em.ObjectController。 ObjectController内容的默认值为null。您应该定义content属性以将值设置为content。下面的代码将起作用

App.LoginController = Ember.ObjectController.extend({
content: {},
actions: {
    login: function() {
        var data = this.getProperties("domain", "username", "password");

        console.log(data);
    }
}
});

答案 1 :(得分:1)

您的属性应在控制器上声明(如果它们被推迟到其模型):

App.LoginController = Ember.ObjectController.extend({
    domain: null,
    username: null,
    password: null,
    actions: {
        login: function() {
            var data = this.getProperties("domain", "username", "password");

            console.log(data);
        }
    }
});