我正在尝试将复选框值(作为 bool 值)插入到我的集合的子模式中。不清楚 1)如何传递复选框值(可以为普通输入字段执行)和 2)如何插入子模式。我正在使用 collection2 和 Handlebars 。
1-这是我需要提交的 HTML 表单:
`<div class="checkbox">
<label><input type="checkbox" id="byow" checked="{{isChecked}}" value="">Bring Your Own Wine</label></div>`
2-这是我在助手(在 Controller 中)中获取表单值和复选框值、提交它并调用将其插入集合的方法:
`BackendController.events({
//Add Venue - Add New Venue Submit Form Helper
'submit #add-venue-form' : function(event) {
event.preventDefault();
var venueName = event.target.venueName.value;
var byow = event.target.byow.checked;
var params = {
venueName: venueName,
byow: byow
}
//Insert Venue
Meteor.call('addVenue', params);
toastr.success('VenueAdded');
Router.go('/admin/manage-venues')
}
3-这是我的方法,被调用以插入我的 Venues 集合(第一部分)以及我的集合和子集合的结构:
`Meteor.methods({
'addVenue': function (params) {
Venues.insert(params);
}
// MAIN SCHEMA for the Venues colleciton.
Schema.Venues = new SimpleSchema({
venueName: {
type: String,
label: "Venue Name",
max: 200,
optional: false
},
//Attach schema for venue attributes (cuisine type, amenities, etc)
venueAttributes: {
type: Schema.VenueAttributes,
optional: true
}
});
//schema for venue attributes. Attached to main schema
Schema.VenueAttributes = new SimpleSchema({
byow: {
type: Boolean,
optional: true
}
});
非常感谢任何帮助 - 我已经设法成功传递 field 名称(因此我的所有权限/发布/订阅都是正确的),但卡在复选框和子集合上。
谢谢! 丹。
最佳答案
最终在 Meteor Chef Slack channel 上的 @lokenx 的帮助下解决了这个问题。
- 按上述方式编写时,复选框代码(第一部分)返回正确的值 (true)。
- 我的错误是我没有将 byow 指定为子属性(将其视为 simpleschema 的根属性)。我应该传递的正确参数是:
var 参数 = {
venuaName: field 名称,
field 属性:{
拜奥:拜奥
}
}
关于javascript - Meteor:将复选框(作为 bool 值)插入到子架构中 (aldeed2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33968327/