Meteor “自动形成”不会渲染“选择选项”字段
我对关系和自动表单的collection2有一个问题。 我尝试实现一个1:n关系,其中每个对象正好有一个objectType,而对于每个objectType,可以引用多个对象 我的模式如下所示:Meteor “自动形成”不会渲染“选择选项”字段,meteor,meteor-autoform,meteor-collection2,simple-schema,Meteor,Meteor Autoform,Meteor Collection2,Simple Schema,我对关系和自动表单的collection2有一个问题。 我尝试实现一个1:n关系,其中每个对象正好有一个objectType,而对于每个objectType,可以引用多个对象 我的模式如下所示: // register collections Objects = new Mongo.Collection('objects'); ObjectTypes = new Mongo.Collection('objectTypes'); // define schema var Schemas = {}
// 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属性有一个select选项字段,但是会出现一个文本输入。有人知道为什么吗
根据文档[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]
编辑
如果我使用
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.
您的类型不是文档中所述的Mongo.Collection实例;这是一个模式。试试这个:
Schemas.Object = new SimpleSchema({
type: {
type: ObjectTypes,
optional: true
},
...
由于没有人能帮我解决这件事,我想出了另一个解决办法:
// 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属性中。由于它返回一个包含标签和值属性的对象数组,autoform将自动呈现一个选择选项