Ember.js 带belongsTo的路由器和DS模型引发混乱异常
我的路由器不断抛出以下异常:Ember.js 带belongsTo的路由器和DS模型引发混乱异常,ember.js,ember-data,Ember.js,Ember Data,我的路由器不断抛出以下异常: Error while processing route: report Assertion Failed: You can only add a 'location' record to this relationship Error: Assertion Failed: You can only add a 'location' record to this relationship DS型号: App.Location = DS.Model.extend({
Error while processing route: report Assertion Failed: You can only add a 'location' record to this relationship Error: Assertion Failed: You can only add a 'location' record to this relationship
DS型号:
App.Location = DS.Model.extend({
street: DS.attr('string'),
city: DS.attr('string'),
area: DS.attr('string'),
lat: DS.attr('string'),
lng: DS.attr('string'),
report: DS.belongsTo('report', {embedded: 'load'})
});
App.Report = DS.Model.extend({
title: DS.attr('string'),
description: DS.attr('string'),
reg_num: DS.attr('string'),
location_str: DS.attr('string'),
location: DS.belongsTo('location', {embedded: 'load'})
});
和我的路由器:
App.ReportRoute = Ember.Route.extend({
model: function () {
return this.store.createRecord('report', {
title: '',
description: '',
reg_num: '',
location_str: '',
location: {
street: '',
city: '',
area: '',
lat: '',
lng: ''
}
});
},
setupController : function(controller, model){
controller.set("model", model);
}
});
在上面的代码中,我使用位置
作为属于报告
的记录
有趣的是,如果我在App.Report中将位置
更改为位置
,则:
Location: DS.belongsTo('location', {embedded: 'load'})
错误消失了。为什么?我仍然在路由器的报告中定义位置
属性,我预期会出现相同的错误
Location
的问题在于请求中出现大写字母:
{
"report": {
"title":"asd",
"description":"asdasd",
"reg_num":"",
"location_str":"novi sad",
"Location":null
}
}
如何设置模型以获取发送到服务器的请求中的位置
编辑
模型保存:
App.ReportController = Ember.ObjectController.extend({
actions: {
saveReport: function(record) {
if (!this.get('title')) {
alert('Title is empty');
} else {
var self = this,
report = this.store.createRecord('report', {
title: this.get('title'),
description: this.get('description'),
reg_num: this.get('reg_num'),
location_str: this.get('location_str'),
location: this.store.createRecord('location', {
street: this.get('street'),
city: this.get('city'),
area: this.get('area'),
lat: this.get('lat'),
lng: this.get('lng')
})
});
console.log(report);
report.save().then(function (result) {
self.transitionToRoute('list');
});
}
}
}
});
您看到的错误是因为在报表路由模型钩子中,您将报表记录上的location属性设置为JavaScript对象文本,而不是ember数据模型
您需要执行以下操作:
model: function () {
return this.store.createRecord('report', {
title: '',
description: '',
reg_num: '',
location_str: '',
location: this.store.createRecord('location', {
street: '',
city: '',
area: '',
lat: '',
lng: ''
}),
});
},
当您将该对象更改为“位置”时,错误就会消失,因为它不再尝试将该对象用作余烬数据模型
至于如何让余烬数据使用大写的位置键,您可以为报表模型创建一个序列化程序,并重写钩子以转换位置键。谢谢,我会尽快尝试返回。这是否等同于在存储中创建报表和位置对象,然后说:report.set('location',location)
?好的,此代码只帮助绕过异常,发送到服务器的请求现在包含位置
,但它总是未定义
。对此有任何建议,我是否遗漏了一些琐碎的内容?