2016-01-08 42 views
0

我有關於關係和autoform的collection2的問題。 我嘗試實現1:n關係,其中每個對象都有一個objectType,而每個objectType可以引用多個對象。autoform不會呈現選擇選項字段

我的架構如下所示:

// 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"}} 

我居然會想到選擇選項字段爲我喜歡的類型屬性,但是,會出現一個文本輸入。有人知道爲什麼

根據文檔[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. 

回答

0

你的類型不是「一個Mongo.Collection實例」之類的文件說;這是一個Schema。試試這個:

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

會拋出一個錯誤,看我更新的問題 – Ronin

0

因爲沒有人可以幫我解決這個事件,我想出了一個替代的解決方案:

// 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將自動呈現選擇選項。