【问题标题】:autoform won't render select option fieldautoform 不会呈现选择选项字段
【发布时间】:2016-04-13 08:26:48
【问题描述】:

我有一个关于 collection2 与关系和自动生成的问题。 我尝试实现 1:n 关系,其中每个对象恰好有 1 个 objectType,而每个 objectType 可以引用多个对象。

我的架构如下所示:

// register collections
Objects = new Mongo.Collection('objects');
ObjectTypes = new Mongo.Collection('objectTypes');

// define schema
var Schemas = {};

Schemas.ObjectType = new SimpleSchema({ // object type schema
    name: {
      type: String
    }
});

Schemas.Object = new SimpleSchema({ // object schema
    type: {
        type: ObjectTypes.Schema,
        optional: true
    },
    title: {
        type: String
    }
});

// attach schemas
ObjectTypes.attachSchema(Schemas.ObjectType);
Objects.attachSchema(Schemas.Object);

我的自动表单如下所示:

{{> quickForm collection="Objects" id="insertTestForm" type="insert"}} 

我实际上希望我的 type 属性有一个选择选项字段,但是会出现一个文本输入。有谁知道为什么?

根据文档[1],它应该是一个选择选项字段:

If you use a field that has a type that is a Mongo.Collection instance, autoform will automatically provide select options based on _id and name fields from the related Mongo.Collection. You may override with your own options to use a field other than name or to show a limited subset of all documents. You can also use allowedValues to limit which _ids should be shown in the options list.

[1]https://github.com/aldeed/meteor-collection2/blob/master/RELATIONSHIPS.md#user-content-autoform

编辑 如果我使用

type: ObjectTypes,

而不是

type: ObjectTypes.Schema,

我的应用程序崩溃,抛出以下错误:

Your app is crashing. Here's the latest log.

/Users/XXX/.meteor/packages/meteor-tool/.1.1.3.ik16id++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:245
                        throw(ex);
                              ^
RangeError: Maximum call stack size exceeded
Exited with code: 8
Your application is crashing. Waiting for file change.

【问题讨论】:

    标签: meteor meteor-autoform meteor-collection2 simple-schema


    【解决方案1】:

    您的类型不是文档所说的“Mongo.Collection 实例”;这是一个架构。试试这个:

    Schemas.Object = new SimpleSchema({
        type: {
            type: ObjectTypes,
            optional: true
        },
        ...
    

    【讨论】:

      【解决方案2】:

      由于没有人可以帮助我解决此事件,我想出了一个替代解决方案:

      // register collections
      Objects = new Mongo.Collection('objects');
      ObjectTypes = new Mongo.Collection('objectTypes');
      
      // define schema
      var Schemas = {};
      
      Schemas.Object = new SimpleSchema({ // object schema
          type: {
              type: String,
              optional: true,
              autoform: {
                  return ObjectTypes.find().map(function(c) {
                      return{label: c.name, value: c._id}
                  });
              }
          },
          // ...
      });
      
      // attach schema
      Objects.attachSchema(Schemas.Object);
      

      如您所见,我手动将我需要的属性从objectTypes 集合映射到autoform 属性。由于它返回一个对象数组,其中包含labelvalue 属性,因此 autoform 将自动呈现一个选择选项。

      【讨论】:

        猜你喜欢
        • 2015-01-31
        • 1970-01-01
        • 1970-01-01
        • 2015-02-02
        • 2019-09-12
        • 2015-02-07
        • 1970-01-01
        • 2022-07-12
        • 2020-10-07
        相关资源
        最近更新 更多