我正在尝试从后端加载一些JSON并启动一个模型。到目前为止,我认为我得到了模型,但没有设置其属性:(
这是我的测试代码:
window.App = Ember.Application.create();
App.Router.map(function() {
this.resource('person', { path: '/people/:person_id' });
});
App.Adapter = DS.RESTAdapter.extend({
namespace: '~user1/embertest'
});
App.Store = DS.Store.extend({
revision: 11,
adapter: App.Adapter,
});
var attr = DS.attr;
App.Person = DS.Model.extend({
firstName: attr('string'),
lastName: attr('string')
});
App.PersonRoute = Ember.Route.extend({
model: function(params) {
return App.Person.find(params) ;
}
});
App.PersonController = Ember.Controller.extend({
contentDidChange: function() {
debugger ;
console.info(this.get('content'));
}.observes('content.isLoaded')
}) ;
数据从json文件加载,该文件只包含:
{"persons": {"firstName": "Jeff","lastName": "Atwood"}}
现在,使用url localhost / ~user / embertest.html #people / 10,我看到数据已加载,并且控制器方法 contentDidChange 被调用。但是当我做的时候
this.content.get("firstName") ; // or this.get("content").get("firstName")
它返回“undefined”。这里出了什么问题?
最后,这是我的模板:
<script type="text/x-handlebars" data-template-name="person">
Person: {{content.firstName}}
</script>
<script type="text/x-handlebars" data-template-name="application">
{{outlet}}
</script>
干杯
答案 0 :(得分:2)
我认为JSON不正确。
{"persons": {"firstName": "Jeff","lastName": "Atwood"}}
在执行App.Person.find(params.person_id)时,根元素应该是“person”
{"person": {"firstName": "Jeff","lastName": "Atwood"}}
我也不相信你需要PersonRoute,因为默认路由应该是:
App.PersonRoute = Ember.Route.extend({
model: function(params) {
return App.Person.find(params.person_id) ;
}
});
当您的动态路径段中包含“* _id”时。
答案 1 :(得分:1)
尝试删除PersonRoute,默认模型钩子应该没问题。我认为这里发生的是App.Person.find(params)发送findQuery()而不是find(id)。