[英]Ember Data: Can't access model data from template
因此,我幾天以來一直對此表示懷疑。 我根本無法在模型中渲染模型數據。 沒有引發任何錯誤,當我在模板中插入{{model}}
或{{controller}}
時,我得到了對DS和Ember對象, <DS.RecordArray:ember411>
和<App.FamilyController:ember438>
。
在Ember Inspector中查看,“數據”選項卡顯示我的記錄加載到各個族中,如果我單擊對象引用,然后單擊“成員/帖子”字段,它也加載了成員/帖子...因此檢查員至少似乎正確地獲取了數據,但是模板卻沒有。 任何幫助,不勝感激。
js / routes / family.js.coffee App.FamilyRoute = Ember.Route.extend model: -> @store.find 'family'
js / templates / family.hbs <img {{bind-attr src=img}} alt="Profile"> <h1>{{name}} Family</h1> <p>{{description}}</p> {{families}} {{controller}}
js / controllers / family.js.coffee App.FamilyController = Ember.ObjectController.extend({})
js / models / family.js.coffee App.Family = DS.Model.extend name: DS.attr('string') description: DS.attr('string') img: DS.attr('string') members: DS.hasMany('member', {async: true}) posts: DS.hasMany('post', {async: true})
API { "families": [{ "id": 2, "name": "Weebleson", "description": "Bob, Jill, Jane, John and Spot.", "img": "/images/weebleson.jpg", "member_ids": [6, 5, 4], "post_ids": [61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80] }] }
API中的 App.Family = DS.Model.extend name: DS.attr('string') description: DS.attr('string') img: DS.attr('string') members: DS.hasMany('member', {async: true}) posts: DS.hasMany('post', {async: true})
json { "families": [{ "id": 2, "name": "Weebleson", "description": "Bob, Jill, Jane, John and Spot.", "img": "/images/weebleson.jpg", "member_ids": [6, 5, 4], "post_ids": [61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80] }] }
Ember : 1.8.1 Ember Data : 1.0.0-beta.14.1 Handlebars : 1.0.0 jQuery : 1.11.1
this.store.find
@store.find
中的this.store.find
或@store.find
返回商店中的所有模型。 因此,默認情況下,您將獲得模型的列表 。 看起來該列表中將有一個家庭。
如果要在列表中顯示,可以使用{{#each family in families}}{{family.name}}{{/each}}
僅在您獲得一定的ID FamilyRoute
,您可以使用@store.find('family', id)
因此,示例為@store.find('family', 2)
。 這將達到另一個終點。
如果要返回第一個結果,則可以使用find
返回的promise:
@store.find('family').then (family) -> family.get('firstObject')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.