我是新来的。我目前使用的是Ember 1.5和Ember Data 1.0.0-beta.7 + canary.b45e23ba和ember-simple-auth 0.2.1。
我正在努力想要对API进行两次自定义调用以进行登录。这是我的代码:
App.ApplicationAdapter = DS.RESTAdapter.reopen
host: '//localhost:8443'
namespace: 'api/v1'
App.store = DS.Store.create
adapter: App.ApplicationAdapter
App.User = DS.Model.extend()
Ember.Application.initializer
name: 'authentication'
initialize: (container, application) ->
container.register('authenticators:custom', App.Authenticator);
Ember.SimpleAuth.setup(container, application)
App.Authenticator = Ember.SimpleAuth.Authenticators.OAuth2.extend
authenticate: (credentials) ->
return new Ember.RSVP.Promise (resolve, reject) ->
Ember.$.ajax
url: '//localhost:8443/api/v1/login'
type: 'POST'
data: { email: credentials.identification, password: credentials.password }
.then (response) ->
token = response.token
userId = response.userId
# Add authorization header
App.ApplicationAdapter.reopen
headers:
Authorization: 'Bearer '+response.access_token
# Load the user data
App.store
.find('user', response.userId)
.then (response) ->
Ember.run ->
resolve({ access_token: token, account_id: userId })
, (xhr, status, error) ->
Ember.run ->
reject(xhr.responseText)
, (xhr, status, error) ->
Ember.run ->
reject(xhr.responseText)
1 /上面的代码到达App.store.find('user', response.userId)
时会失败。投掷:
Uncaught TypeError: Cannot read property 'normalize' of undefined ember-data.js:9806
Ember.Object.extend.modelFor ember-data.js:9806
Ember.Object.extend.findById ember-data.js:9098
Ember.Object.extend.find ember-data.js:9085
(anonymous function) initializers.coffee:30
(anonymous function) jquery.js:3206
fire jquery.js:3049
self.fireWith jquery.js:3161
done jquery.js:8185
(anonymous function) jquery.js:8532
2 /我对使用带有Ember Data 1.0的RESTAdapter获取商店所需的内容感到困惑。显然API已更改,我不知道我的App.store
是否是实例化商店的正确方法。
感谢您的帮助!
答案 0 :(得分:0)
用这个替换你的发现:
container.lookup('store:main').find('user', response.userId)