1

ember-cli 中工厂的正确位置在哪里。一旦我有了工厂的正确位置,我是否在我的初始化程序中注册

container.register('authenticator:custom', application.CustomAuthenticator);

// the custom authenticator that authenticates the session against the custom server
App.CustomAuthenticator = Ember.SimpleAuth.Authenticators.Base.extend({
    tokenEndpoint: '/v4/session',

    restore: function(data) {
        return new Ember.RSVP.Promise(function(resolve, reject) {
            if (!Ember.isEmpty(data.token)) {
                resolve(data);
            } else {
                reject();
            }
        });
    },

    authenticate: function(credentials) {
        var _this = this;
        return new Ember.RSVP.Promise(function(resolve, reject) {
            Ember.$.ajax({
                url:         _this.tokenEndpoint,
                type:        'POST',
                data:        JSON.stringify({ session: { identification: credentials.identification, password: credentials.password } }),
                contentType: 'application/json'
            }).then(function(response) {
                Ember.run(function() {
                resolve({ token: response.session.token });
            });
        }, 
            function(xhr, status, error) {
                var response = JSON.parse(xhr.responseText);
                Ember.run(function() {
                    reject(response.error);
                });
            });
        });
    },

    invalidate: function() {
        var _this = this;
        return new Ember.RSVP.Promise(function(resolve) {
            Ember.$.ajax({ url: _this.tokenEndpoint, type: 'DELETE' }).always(function() {
                resolve();
            });
        });
    },
});

谢谢!

4

1 回答 1

0

是的,你就是这样做的。确保在您跌倒 Ember.SimpleAuth.setup 之前调用 container.register。

于 2014-06-11T08:16:53.183 回答