我使用Ember.js和ember-data爲自制API創建一個GUI。如何使用Ember Data獲取保存的返回數據?
我一直在關注這個tutorial來處理身份驗證,但我想使用ember-data而不是自定義的jQuery請求。
我必須做的一件事就是通過發送電子郵件和密碼來調用API來創建一個新的會話,並且API會將我發回給API Key對象。
這裏是我的LoginController處理loginUser行動(這是CoffeeScript的):
App.LoginController = Ember.ObjectController.extend
actions:
loginUser: ->
session = @store.createRecord 'session',
email: @get 'email'
password: @get 'password'
session.save()
下面是結果創建會話時,我得到:
{
"users": [
{
"id": "525fa0286c696c0b14040000",
"email": "[email protected]",
"first_name": "John",
"surname": "Doe"
}
],
"api_key": {
"id": "526e464c6c696c07d2000000",
"type": "session",
"key": "6b824d6a-a065-4b6f-bb28-5c19389760f8",
"expires_at": "2013-10-28T11:41:08+00:00",
"user_id": "525fa0286c696c0b14040000"
}
}
我有會議,ApiKey和用戶模型。我可以創建會話,但我不明白的是如何訪問save()
方法的返回值。
我知道我的ApiKey和用戶被加載到某個地方,因爲如果他們各自的Ember模型不存在但我不知道如何訪問它們,我在save()
之後得到一個錯誤。
我試過使用save()
回調,如then()
或didCreate
事件,但缺少有關傳遞給這些回調的參數的文檔以及如何使用它們。
- Ember.js 1.1.2
- 灰燼數據1.0.0.beta.3
編輯: 我試着上創建actuel會話模型我的API,導致這個JSON輸出:
{
"api_keys": [
{
"id": "526f69526c696c07d2110000",
"type": "session",
"key": "4c26af37-2b21-49c2-aef5-5850a396da0b",
"expires_at": "2013-10-29T08:22:50+00:00",
"user_id": "525fa0286c696c0b14040000"
}
],
"users": [
{
"id": "525fa0286c696c0b14040000",
"email": "[email protected]",
"first_name": "John",
"surname": "Doe"
}
],
"session": {
"id": "526f6e666c696c18c0010000",
"api_key_id": "526f69526c696c07d2110000"
}
}
(注意根元素是現在session
)
它沒有更好的工作,因爲現在我的頭攢動導致以下錯誤(不是在控制檯,但then
點錯誤回調):
Object function() { [...] } has no method 'eachTransformedAttribute'
我得到這個錯誤,在Ember數據模型中聲明的Session和ApiKey之間的關係與否...