如何 save/update 集合中的对象和对象中的嵌入对象?
How to save/update object in collection and embed object in object?
我有两个系列“颜色”和“汽车”。
在车上可以选择颜色。
如何save/update集合中的对象,以便将Color对象嵌入到Car对象中?
Cars = new Mongo.Collection('cars');
Cars.attachSchema(new SimpleSchema({
colorId: {
label: 'Color',
type: String,
autoform: {
options: function () {
return Colors.find().map(function (p) {
return {label: p.colorName, value: p._id};
});
},
label: false
}
},
color: {
type: Object,
},
'color._id': {
type: String,
autoform: {
omit: true,
},
},
'color.colorName': {
type: String,
autoform: {
omit: true,
},
},
'color.colorCode': {
type: String,
autoform: {
omit: true,
},
},
}));
Colors = new Mongo.Collection('colors');
Colors.attachSchema(new SimpleSchema({
colorName: {
type: String,
label: "Color Name",
max: 20,
},
colorCode: {
type: String,
optional: true,
label: "Color Code",
autoform: {
afFieldInput: {
type: "color"
}
}
},
}));
我试试用
AutoForm.hooks({ insertCarForm: {之前: {
但是没用
有多种方法可以实现此目的,解决方案在很大程度上取决于您可能使用的任何相关包。如果没有看到创建新 'cards' 的现有代码,很难给出一个工作示例。不过,这里有一个使用核心 Meteor API.
的示例
- 假设你定义了一些表单模板(我称之为 'manageCar'),你会做这样的事情。
定义一个 Meteor 方法来处理 inserting/updating 汽车。
Meteor.methods({
updateCar: function(carDoc) {
check(carDoc, { /* carDoc schema */ });
const color = Colors.findOne(carDoc.colorId);
carDoc.color = color;
if (carDoc._id) {
Cars.update(carDoc._id, {
$set: {
colorId: carDoc.colorId,
color: carDoc.color,
}
})
} else {
Cars.insert(carDoc);
}
},
});
为调用已定义方法的表单提交添加事件处理程序。
Template.manageCar.events({
'click .js-save-car'(event, instance) {
const data = {
_id: event.target._id.value,
colorId: event.target.colorId.value
};
Meteor.call('updateCar', data, function(error, result) {
if (!error) {
alert('Car updated successfully');
}
});
}
});
长话短说,您只需要确保您可以访问为汽车保存的颜色 ID,然后确保对颜色集合执行查找以检索必要的颜色文档,然后使用为您的汽车插入或更新。
如果您有任何问题或需要进一步解释,请告诉我。
我有两个系列“颜色”和“汽车”。 在车上可以选择颜色。
如何save/update集合中的对象,以便将Color对象嵌入到Car对象中?
Cars = new Mongo.Collection('cars');
Cars.attachSchema(new SimpleSchema({
colorId: {
label: 'Color',
type: String,
autoform: {
options: function () {
return Colors.find().map(function (p) {
return {label: p.colorName, value: p._id};
});
},
label: false
}
},
color: {
type: Object,
},
'color._id': {
type: String,
autoform: {
omit: true,
},
},
'color.colorName': {
type: String,
autoform: {
omit: true,
},
},
'color.colorCode': {
type: String,
autoform: {
omit: true,
},
},
}));
Colors = new Mongo.Collection('colors');
Colors.attachSchema(new SimpleSchema({
colorName: {
type: String,
label: "Color Name",
max: 20,
},
colorCode: {
type: String,
optional: true,
label: "Color Code",
autoform: {
afFieldInput: {
type: "color"
}
}
},
}));
我试试用 AutoForm.hooks({ insertCarForm: {之前: {
但是没用
有多种方法可以实现此目的,解决方案在很大程度上取决于您可能使用的任何相关包。如果没有看到创建新 'cards' 的现有代码,很难给出一个工作示例。不过,这里有一个使用核心 Meteor API.
的示例- 假设你定义了一些表单模板(我称之为 'manageCar'),你会做这样的事情。
定义一个 Meteor 方法来处理 inserting/updating 汽车。
Meteor.methods({
updateCar: function(carDoc) {
check(carDoc, { /* carDoc schema */ });
const color = Colors.findOne(carDoc.colorId);
carDoc.color = color;
if (carDoc._id) {
Cars.update(carDoc._id, {
$set: {
colorId: carDoc.colorId,
color: carDoc.color,
}
})
} else {
Cars.insert(carDoc);
}
},
});
为调用已定义方法的表单提交添加事件处理程序。
Template.manageCar.events({
'click .js-save-car'(event, instance) {
const data = {
_id: event.target._id.value,
colorId: event.target.colorId.value
};
Meteor.call('updateCar', data, function(error, result) {
if (!error) {
alert('Car updated successfully');
}
});
}
});
长话短说,您只需要确保您可以访问为汽车保存的颜色 ID,然后确保对颜色集合执行查找以检索必要的颜色文档,然后使用为您的汽车插入或更新。
如果您有任何问题或需要进一步解释,请告诉我。