2017-03-07 26 views
0

請告訴我如何從afQuickField輔助方法訪問scheam定義。我想根據類型進行不同的渲染。Meteor.js:如何在輔助函數中從afQuickField訪問模式元數據(字段類型)

可以說我有定義

mySchema = new SimpleSchema({ 
numberField: { 
    type: Number, 
    defaultValue: 3 
}, 
stringField: { 
    type: String, 
},... 

我想告訴我,我現在用的字段的類型的輔助方法的簡單模式。

Template.myQuickField.helper({ 
    fieldMetaDataString: function() { 
    let type = ??What to put here. Code that looks at Simple Schema definition for the field and tells me its type?? 
    if(type===String){ 
    return true; 
    } 
    return false; 

} });

現在我想將afQuickField包裝到我自己的模板中,以便根據字段的類型對其進行不同的呈現。請注意,這是一個簡化的例子。我想做更多,然後改變風格。

<template name="myQuickField"> 
    {{#if fieldMetaDataString}} 
    {{> afQuickField id=id name=name style="stringstuff"}} 
    {{else}} 
    {{> afQuickField id=id name=name style="otherstuff"}} 
    {{/if}} 

編輯: 我想我應該然後我使用了自動的myQuickField說。 ,我也會在其他館藏中使用它。所以我也需要知道如何從字段helperMethod中的自動窗體中找出集合的值。

<template name="myForm"> 
{{#autoForm collection="mySchema" type="insert"}} 
<fieldset> 
    {{> **myQuickField** name='numberField'}} 
    {{> **myQuickField** name='stringField'}} 

</fieldset> 
{{/autoForm}} 
</template> 

回答

0

是這樣的?

Myschemadef ={ 
numberField: { 
    type: Number, 
    defaultValue: 3 
}, 
stringField: { 
    type: String, 
} 
} 

mySchema = new SimpleSchema(Myschemadef); 

Template.myQuickField.helper({ 
    fieldMetaDataString: function(feld) { 
    let type = Myschemadef[feld].type; 
    return type===String; 
} }); 
0

您可以隨時導出模式您連接之前,然後在模板js文件導入。

export const schema = new SimpleSchema({ 
    stringField: { 
    type: String 
    }, 
    numberField: { 
    type: Number 
    } 
}); 

然後再導入

import { schema } from '../imports/api/data/data.js' 
Template.myQuickField.helper({ 
    setStyle: function(name) { 
    let type = schema._schema[name].type 
    if(type===String){ 
    return "stringStyle"; 
    } else if (type===Number){ 
    return "numberStyle" 
    } 
}); 

內myQuickField模板

{{> afQuickField id=id name={{name}} style="{{setStyle name}}"}} 

了自動模板是一樣的

<template name="myForm"> 
{{#autoForm collection="mySchema" type="insert"}} 
<fieldset> 
    {{> myQuickField name='numberField'}} 
    {{> myQuickField name='stringField'}} 

</fieldset> 
{{/autoForm}} 
</template> 
+0

請參閱我的最後的編輯。我不認爲我第一次正確解釋了我的需要。我需要一個幫助器方法來查找集合和字段。有沒有辦法從myQuickField.helpers –

+0

來訪問autoforms集合變量ya,它是一樣的,但不是靜態字符串,而是傳遞name參數 – mutdmour

相關問題