[英]Ember Data: Can't access model data from template
So I've been beating my head against this for a few days now. 因此,我几天以来一直对此表示怀疑。 I can't get my model data to render in the template at all.
我根本无法在模型中渲染模型数据。 No errors are being thrown, and when I insert
{{model}}
or {{controller}}
in the template, I get a reference to the DS and Ember objects, <DS.RecordArray:ember411>
, and <App.FamilyController:ember438>
, respectively. 没有引发任何错误,当我在模板中插入
{{model}}
或{{controller}}
时,我得到了对DS和Ember对象, <DS.RecordArray:ember411>
和<App.FamilyController:ember438>
。
Looking in the Ember Inspector, the Data tab shows my record loaded in families, and if I click through to the object reference, and into the members/posts field, it loads the members/posts in too... so the inspector at least seems to be getting the data correctly, but the template doesn't. 在Ember Inspector中查看,“数据”选项卡显示我的记录加载到各个族中,如果我单击对象引用,然后单击“成员/帖子”字段,它也加载了成员/帖子...因此检查员至少似乎正确地获取了数据,但是模板却没有。 Any help much appreciated.
任何帮助,不胜感激。
js/routes/family.js.coffee App.FamilyRoute = Ember.Route.extend model: -> @store.find 'family'
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 / 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 / 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})
json from 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] }] }
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
or @store.find
in the coffeescript case returns all the models in the store. this.store.find
@store.find
中的this.store.find
或@store.find
返回商店中的所有模型。 So, you are getting a list back for your model by default. 因此,默认情况下,您将获得模型的列表 。 It looks like there will be one family in that list.
看起来该列表中将有一个家庭。
If you want to display it in a list, you could use {{#each family in families}}{{family.name}}{{/each}}
如果要在列表中显示,可以使用
{{#each family in families}}{{family.name}}{{/each}}
To only get a certain ID in your FamilyRoute
, you can use @store.find('family', id)
. 仅在您获得一定的ID
FamilyRoute
,您可以使用@store.find('family', id)
So an example would be @store.find('family', 2)
. 因此,示例为
@store.find('family', 2)
。 This would hit a different endpoint. 这将达到另一个终点。
If you want to return the first result, you can use the promise that find
returns: 如果要返回第一个结果,则可以使用
find
返回的promise:
@store.find('family').then (family) -> family.get('firstObject')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.