问题描述
我正在使用Ember数据,并很难弄清楚从服务器获取的垃圾回收信息可以在JSON响应中识别嵌套的属性。这是ember-1.0.0-pre.4.js。
I'm using Ember data and having a hard time figuring out get ember to recognize nested properties in my JSON response from the server. This is ember-1.0.0-pre.4.js.
目前,我已经设置了与Ember数据版本11相关联的模型。这里
Currently, I've set up associated models with Ember data revision 11. Here the
# School Model
App.School = DS.Model.extend
addr: DS.belongsTo('App.Addr')
name: DS.attr 'string'
status: DS.attr 'string'
# Address Model
App.Addr = DS.Model.extend
school: DS.belongsTo 'App.School'
line1: DS.attr 'string'
city: DS.attr 'string'
state: DS.attr 'string'
iso: DS.attr 'string'
以下是我的服务器的JSON响应:
And here is the JSON response from my server:
{"schools":
[{
"_id":"51020261bbc3b8c526000007",
"name":"Willamette",
"status":"p",
"addr":{
"line1":"122 Evergreen Terrace",
"city":"Springfield",
"state":"IL",
"iso":"US"
}
}
]}
我的适配器设置如下:
App.Store = DS.Store.extend
revision: 11
adapter: DS.RESTAdapter.create({
url: "http://localhost:8000/api"
serializer: DS.RESTSerializer.extend
primaryKey: (type) ->
'_id';
})
更新:
我试图映射addr属性, 如下。仍然没有骰子...
UPDATE:I've tried to map the addr property, as follows. Still no dice...
DS.RESTAdapter.map 'App.School',
addr: { embedded: 'always'}
在我的模板中,我想做这样的事情...
In my template, I'd like to do something like this...
<script type="text/x-handlebars" data-template-name="school">
<h2>School: {{name}}</h2>
<p> Status: {{ status }}</p>
<p> Address: {{ addr.line1 }} {{ addr.city }} </p>
</script>
名称和状态属性呈现良好。但是addr.line1和addr.city是空白的。有没有办法让Ember识别嵌套的addr属性?
The name and status properties render fine. But addr.line1 and addr.city are blank. Is there a way to get Ember to recognize the nested addr propeties?
谢谢!
推荐答案
您是否有时间查看
它允许嵌套数据
这篇关于如何使用Ember数据访问json中的嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!