2014-01-25 44 views
1

我試圖使用Ember Data填充模板。 當我嘗試在DS Store中查找模型時,出現奇怪的問題。 我跟着一些教程,但有一個惱人的錯誤。Ember Data:無法從數據存儲中獲取模型

錯誤是'加載路徑時出錯:未定義'。

我已經試過:

MovieTracker.Store = DS.Store.extend({ 
    url: 'http://addressbook-api.herokuapp.com' 
}); 

MovieTracker.Contact = DS.Model.extend({ 
    first: DS.attr('string'), 
    last: DS.attr('string'), 
    avatar: DS.attr('string') 
}); 

MovieTracker.Router.map(function() { 
    this.resource('contacts'); 
}); 

MovieTracker.ContactsRoute = Ember.Route.extend({ 
    model: function(){//works when changing to 'activate:' 
     //return; //this works! it shows me a simple template and updates URL to index.html#/contacts 
     return this.store.find('contact');//error: 'Error while loading route: undefined' 
    } 
}); 

在的Index.html我有一個簡單#鏈接到「通訊錄」(應用車把),效果很好。 我也有一個叫聯繫人的簡單模板,當我放棄this.store.find('contact')這一行時,它工作正常。

JSBin:http://emberjs.jsbin.com/OxIDiVU/170/edit?html,js,output 的JSON是:http://addressbook-api.herokuapp.com/contacts

能否請你給我什麼建議? 你更喜歡Ember Data(1.0 Beta 5)嗎? 另一個問題:沒有預編譯車把的網站是不是一個好主意?

非常感謝您的閱讀!

+0

從服務器正確的反應如何?你可以發佈服務器的響應嗎?讓Ember數據工作是有點問題,但一旦一切都解決了,它確實工作得很好。我在Ember.js和Ember數據上設置了一個相當大的網站。 – Gogu

+0

嗨Gogu,當然,這是我使用的spesific網址:http://addressbook-api.herokuapp.com。 Ember Data實際上應該訪問此網站/聯繫人,並獲取JSON。我希望我能成功設置好你的一切,謝謝! – TechWisdom

+0

我一直在玩,並發現如果我將ContactsRou​​te中的'model:'屬性更改爲'activate',它會越來越好。錯誤沒有顯示,但另一個錯誤叫我'關於必須是數組'的循環。我不知道將「模型」更改爲「激活」的意義,但希望它能幫助您瞭解錯誤的起源。 – TechWisdom

回答

1

在定義主機時,您在適配器上定義該主機,而不是在商店中定義該主機。

MovieTracker.ApplicationAdapter = DS.RESTAdapter.extend({ 
    host: 'http://addressbook-api.herokuapp.com' 
}); 

此外,你不應該定義模型上的ID,它的存在默認

MovieTracker.Contact = DS.Model.extend({ 
    first: DS.attr('string'), 
    last: DS.attr('string'), 
    avatar: DS.attr('string') 
}); 

http://emberjs.jsbin.com/OxIDiVU/172/edit

而且燼數據的新版本都沒有記錄在網站上但是,過渡文件應該有助於解釋一些細微差別和變化。

https://github.com/emberjs/data/blob/master/TRANSITION.md

+0

非常感謝!它絕對解決了我的問題!感謝Gogu和Deewendra的努力! – TechWisdom

相關問題