首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Quickform / Autoform更新嵌套对象数组

Quickform / Autoform更新嵌套对象数组
EN

Stack Overflow用户
提问于 2015-04-15 22:55:04
回答 1查看 1.8K关注 0票数 2

我在使用autoForm向集合中添加新的嵌套/数组值时遇到了困难。

我正在尝试使用quickForm更新问题。我希望用户能够添加更多的答案选项。我的模式如下(简化为省略顺序、一些元数据等):

代码语言:javascript
复制
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空了。所以我把它们藏起来了,但让它们以形式存在。

我的编辑表单如下所示:

代码语言:javascript
复制
{{> quickForm collection="Questions" id="editQuestionForm"
    type="update" setArrayItems="true" doc=questionToEdit
    fields="label, answers"}}

我目前能够更新标签,为我的问题和任何答案,我最初添加。但我不能增加新的答案。当我这样做时,它被拒绝了,因为count不是可选的。但我指定了一个defaultValue..。

我希望我的quickForm看起来像这样,这样我就不会把count_id放在用户可以更改它们的地方了:

代码语言:javascript
复制
{{> quickForm collection="Questions" id="editQuestionForm"
    type="update" setArrayItems="true" doc=questionToEdit
    fields="label, answers, answers.$.label"}}

但是,也许我需要将answers.$._id保存在那里并隐藏起来,以确保更改更新正确的答案?

所以:

  1. 我的答案在insert上默认为0,那么为什么我在编辑和添加答案时没有这样做呢?
  2. autoForm可以做一个插入而不是更新吗?插入新问题,更新现有问题标签,根据需要使用defaultalueautoValue
  3. 我应该用一种方法来处理这类事情吗?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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:

代码语言:javascript
复制
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");
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29662366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档