Javascript 需要将模型注入服务,Ember JS

Javascript 需要将模型注入服务,Ember JS,javascript,ember.js,Javascript,Ember.js,需要帮助 我是Ember的新手,正在尝试在CLI中使用身份验证服务进行登录/注销。。。然而,我似乎无法调用任何方便的ORM帮助程序而不得到一个未定义的错误 我确实使用了传统的jqueryajax,但我真的很想使用rest适配器 服务/身份验证.JS import Ember from 'ember'; import DS from 'ember-data'; import App from '../app.js'; import User from '../models/user.js'; im

需要帮助

我是Ember的新手,正在尝试在CLI中使用身份验证服务进行登录/注销。。。然而,我似乎无法调用任何方便的ORM帮助程序而不得到一个未定义的错误

我确实使用了传统的jqueryajax,但我真的很想使用rest适配器

服务/身份验证.JS

import Ember from 'ember';
import DS from 'ember-data';
import App from '../app.js';
import User from '../models/user.js';
import ApiKey from '../models/api-key.js';

export default Ember.Object.extend({

//load the current user if the cookies exist and is valid
init: function() {
    this._super();
    var accessToken = Ember.$.cookie('access_token');
    var authUserId = Ember.$.cookie('auth_user');
    if (!Ember.isEmpty(accessToken) && !Ember.isEmpty(authUserId)) {
        this.authenticate(accessToken, authUserId);
    }
 },

//determine if the user is currently authenticated
isAuthenticated: function() {
    return !Ember.isEmpty(this.get('apiKey.accessToken')) &&       !Ember.isEmpty(this.get('apiKey.user'));
},

// Auth the user. Once they are auth, set access token to be submitted    with all
// future AJAX requests 
authenticate: function(accessToken, userId) {
    Ember.$.ajaxSetup({
        headers: { 'Authorization': 'Bearer ' + accessToken }
    });
    user = User.find(userId)
    this.set('apiKey', ApiKey.create({
     accessToken: accessToken,
     user: user
    }));
   },

   //log out the user 
   reset: function({
App.__container__.lookup('route:application').transitionTo('session.new');
    Ember.run.sync();
    Ember.run.next('apiKey', null);
    Ember.$.ajaxSetup({
        headers: { 'Authorization' : 'Bearer none' }
    });
},

// Ensure that when the browser the cookies are refreshed 
apiKeyObserver: function() {
    if (Ember.isEmpty(this.get('apiKey'))) {
        Ember.$.removeCookie('access_token');
        Ember.$.removeCookie('auth_user');
    } else {
        Ember.$.cookie('access_token', this.get('apiKey.accessToken'));
        Ember.$.cookie('auth_user', this.get('apiKey.user.id'));
    }
}
});

// // Reset the authentication if any ember data request returns a 401     unauthorized error
DS.rejectionHandler = function(reason) {
  if (reason.status === 401) {
    App.AuthManager.reset();
  }
throw reason;
};

您正在尝试注入模型的代码,而不是实例。试试这个.store.find('user')Vaibhav的评论是正确的。我还建议使用with做你正在做的事情,但也免费提供了一大堆好东西!我决定在我的项目进行了6个月后使用它,我希望我能早点找到它:)谢谢大家。我查看了ember simple auth存储库,这似乎是一个更好的解决方案!考虑回答你的问题,如果你已经找到了解决方案。ValiabHv解决方案工作:但是,为了简单起见,我决定去Engor Simple AuthAuthor作为我的AuthApple!