假设我有以下模型:
// customer.js
DS.Model.extend({
products: DS.hasMany('product')
});
// product.js
DS.Model.extend({
customer: DS.belongsTo('customer')
});我需要创建一个具有I(尚未从后端加载的)产品列表的客户,如下所示:
this.get('store').createRecord('customer', {products: [1, 2, 3]}); 但是这失败了,因为商店希望产品是DS.Model的数组:
处理路由时出错:索引断言失败: hasMany关系的所有元素都必须是DS.Model的实例,您传递了1,2,3
如何使用ID提供的关联创建记录?
发布于 2017-03-26 16:11:07
处理路由时出错:索引断言失败: hasMany关系的所有元素都必须是DS.Model的实例,您传递了1,2,3错误
由于错误声明您需要传递DS.Model实例,但是您只能使用createRecord创建实例,因此您可能需要执行如下所示的操作:
let product1 = this.store.createRecord('product',{customer:1});
let product2 = this.store.createRecord('product',{customer:2});
return this.get('store').createRecord('customer', {products:[product1,product2]});发布于 2017-03-28 15:58:31
如果相关记录不存在,那么您就不能使用If动态创建它们。但是,您可以这样做:
let customer = this.store.createRecord('customer', {});
customer.get('products').then(function() {
customer.get('products').addObject(this.store.createRecord('product', {/* ... */}));
});https://stackoverflow.com/questions/42964727
复制相似问题