2013-01-24 166 views
0

我購買的Ember.js的發揮發揮在peepcode.com,隨後爲所提供的視頻。灰燼數據DS.Model.find()不似乎工作

所以,我在Rails的設置模式和串行和控制器了。

當我這樣鍵入URL。

http://localhost:3000/actors/wycats 

呈現爲我預期的JSON響應。 (這也可能是子玉數據的預期JSON form..right?)

{ 
    "actor": { 
    "id": 1, 
    "blog": "http://www.yehudakatz.com", 
    "company": "Tilde, Inc.", 
    "email": "[email protected]", 
    "gravatar_id": "428167a3ec72235ba971162924492609", 
    "location": "San Francisco", 
    "login": "wycats", 
    "name": "Yehuda Katz", 
    "actor_type": "User" 
    } 
} 

所以我成立了ember.js

GithubScore.Store = DS.Store.extend({ 
    revision: 11, 
    adapter: "DS.RESTAdapter" 
}); 


GithubScore.Actor = DS.Model.extend({ 
    login: DS.attr('string'), 
    name: DS.attr('string'), 
    gravatarId: DS.attr('string'), 
    location: DS.attr('string'), 
    blog: DS.attr('string') 
}); 

Store和角色模型,我開始了我的灰燼應用程序,沒有錯誤發生。

但是當我試圖使用控制檯 得到一個模型(我已經保存在Rails的DB ID爲1的模型)

GithubScore.Actor.find(1) 

它返回一個類,沒有出現錯誤,但是當我嘗試獲取它的一個屬性。 只返回空值,雖然模型的狀態「isLoaded」

GithubScore.Actor.find(1).get('isLoaded') 
=> true 

GithubScore.Actor.find(1).get('blog') 
=> null 

,我發現,當我打電話GithubScore.Actor.find(1)獲得(「isLoaded」)屢在第一次只返回假,但是當我嘗試獲得一個屬性'isLoaded'時立即更改爲true。

GithubScore.Actor.find(1).get('isLoaded') 
=> false (for many times) 

GithubScore.Actor.find(1).get('blog') 
=> null 

GithubScore.Actor.find(1).get('isLoaded') 
=> true (immediately changed) 

當我嘗試使用.toJSON()方法來建模視頻。它會引發錯誤。

GithubScore.Actor.find(1).toJSON() 
=> TypeError: Object <GithubScore.Actor:ember272:1> has no method 'toJSON' 

有一件事我好奇的是,雖然GithubScore.Store對象從DS.Store延長。

它沒有找哪家DS.store已經有(類型,ID)方法。我找不到問題所在。你會給我一些幫助嗎?

謝謝您的閱讀!

回答

3

嘗試,而不是顯示在模板的博客價值,並通過瀏覽器訪問它。當你執行GithubScore.Actor.find(1).get('blog')時,它會返回null,因爲Ember只是將一個對象重新調整爲一個Promise,同時仍然在取回對象。當您嘗試改爲在模板中顯示值時,模板將綁定到該值並在檢索後更新。

至於isLoaded = true,顯然這是一個錯誤,我遇到了與RecordArray相同的問題,並且在其他人的計算機上也報告過previous question