2014-06-10 37 views
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(); 
      }); 
     }); 
    }, 
}); 

谢谢!

回答

0

是的,你就是这么做的。确保在您落入Ember.SimpleAuth.setup之前调用container.register。