Ember.js ember js为包含数组的对象定义模型
这是我的代码,我定义了一个包含数组的模型,问题是当我将它发送到服务器时,它不会发送数组Ember.js ember js为包含数组的对象定义模型,ember.js,Ember.js,这是我的代码,我定义了一个包含数组的模型,问题是当我将它发送到服务器时,它不会发送数组 import DS from 'ember-data'; export default DS.Model.extend({ titre: DS.attr('string'), categorie: DS.attr('string'), introduction: DS.attr('string'), chapitres: [{ titre: DS.attr('string'), introd
import DS from 'ember-data';
export default DS.Model.extend({
titre: DS.attr('string'),
categorie: DS.attr('string'),
introduction: DS.attr('string'),
chapitres: [{
titre: DS.attr('string'),
introduction: DS.attr('string')
sousChapitres: [{
contenu: DS.attr('string'),
}],
exercices: [{
contenu: DS.attr('string')
}],
conclusion: DS.attr('string')
}]
});
谢谢,您必须为子结构创建模型:
export default DS.Model.extend({
titre: DS.attr('string'),
categorie: DS.attr('string'),
introduction: DS.attr('string'),
chapitres: DS.hasMany('chapitre'),
});
//models/chapitres
export default DS.Model.extend({
titre: DS.attr('string'),
introduction: DS.attr('string'),
exercices: DS.hasMany('exercice'),
});
//models/exercices
export default DS.Model.extend({
contenu: DS.attr('string'),
});
您也可以执行
chapitres:DS.attr()
,它将保留已与该键关联的任何结构。感谢您的响应,此方法不会将数组的内容发送到服务器,下面是我发送{'cour'时的一个示例,{titre:'titre',categorie:'categorie',introduction:'introduction',chapitres:[{titre:'string'}]}];“我是在这个服务器上的”{cour:{titre:'titre',categorie:'categorie',introduction:'introduction',chapitres:[Object]},'谢谢你的回复,我照你说的做了,我收到了这个错误消息,“处理路由时出错:cours.nouveau断言失败:余烬数据需要一个数字或字符串来表示记录”相反,它在chapitres
关系中找到了一个对象。如果这是一个多态关系,请指定type
键。如果这是一个嵌入关系,请包括DS.EmbeddedRecordsMixin
并在序列化程序的attrs对象中指定chapitres
属性。”因此,我为模型添加了一个序列化程序,如下所示:从“ember data”导入DS;导出默认的DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin,{attrs:{chapitres:{embedded:'always'},});我得到了这个错误::处理路由时出错:cours.nouveau断言失败:您必须在传递给push
的对象中包含chapitre的id。所有记录都需要id。很简单。