问题描述
我正在使用 Ember 数据并且很难弄清楚让 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 中的嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!