我在使用autoForm向集合中添加新的嵌套/数组值时遇到了困难。
我正在尝试使用quickForm更新问题。我希望用户能够添加更多的答案选项。我的模式如下(简化为省略顺序、一些元数据等):
questionSchema = new SimpleSchema({
label: {
type: String
},
answers: {
type: Array,
minCount: 2,
maxCount: 6
},
"answers.$": {
type: Object
},
"answers.$._id": {
type: String,
regEx: SimpleSchema.RegEx.Id,
autoValue: function(){ return Random.id(); },
autoform: {
type: "hidden"
}
},
"answers.$.label": {
type: String,
regEx: /.{1,150}/,
autoform: {
label: false
}
},
"answers.$.count": {
type: Number,
defaultValue: 0,
autoform: {
type: "hidden"
}
}
});除了answers.$.label之外,当我只是通过quickForm type='insert'添加问题时,我没有使用任何autoform选项。当我想编辑问题时,我添加了这些选项,因为否则我会收到一个抱怨,说我把count空了。所以我把它们藏起来了,但让它们以形式存在。
我的编辑表单如下所示:
{{> quickForm collection="Questions" id="editQuestionForm"
type="update" setArrayItems="true" doc=questionToEdit
fields="label, answers"}}我目前能够更新标签,为我的问题和任何答案,我最初添加。但我不能增加新的答案。当我这样做时,它被拒绝了,因为count不是可选的。但我指定了一个defaultValue..。
我希望我的quickForm看起来像这样,这样我就不会把count或_id放在用户可以更改它们的地方了:
{{> quickForm collection="Questions" id="editQuestionForm"
type="update" setArrayItems="true" doc=questionToEdit
fields="label, answers, answers.$.label"}}但是,也许我需要将answers.$._id保存在那里并隐藏起来,以确保更改更新正确的答案?
所以:
defaultalue或autoValue。发布于 2015-04-16 04:07:08
编辑:我已经更新了示例,并将测试应用程序部署到http://test-questions.meteor.com/的metoer中。它的边缘有点粗糙(老实说,它一点用处都没有),但它应该显示实际的功能。使用底部的“添加一个新问题”链接。现有的问题应该出现在加法形式的底部。单击现有的问题来编辑它。总的来说,功能是存在的。别因为我设计不好而恨我。怪罪时间女神。
我通常做嵌入文档的方法是将每个子对象划分为一个单独的模式。这使代码保持整洁,易于理解,同时也避免了典型的缺陷。
这里是一个示例项目,演示了下面的shema在行动中的作用。只是git拉和流星运行:https://github.com/nanlab/question
新链路http://app-bj9coxfk.meteorpad.com/
代码:http://meteorpad.com/pad/7fAH5RCrSdwTiugmc/
question.js:
Questions = new Mongo.Collection("questions");
SimpleSchema.answerSchema = new SimpleSchema({
_id: {
type: String,
regEx: SimpleSchema.RegEx.Id,
autoValue: function() {
return Random.id();
},
autoform: {
type: "hidden"
}
},
label: {
type: String,
regEx: /.{1,150}/,
autoform: {
label: false
}
},
count: {
type: Number,
autoValue: function() {
return 0;
},
}
})
Questions.attachSchema(new SimpleSchema({
label: {
type: String
},
answers: {
type: [SimpleSchema.answerSchema],
minCount: 2,
maxCount: 6
},
}))
Questions.allow({
insert: function(){return true;},
update: function(){return true;},
})
if(Meteor.isServer) {
Meteor.publish("questions", function() {
return Questions.find();
})
} else {
Meteor.subscribe("questions");
}https://stackoverflow.com/questions/29662366
复制相似问题