2013-08-16 22 views
0

此刻我嘗試在grails中處理腳手架。在那裏我有一個問題,我沒有找到解決方案的代模板。我想在domainmodel中配置生成算法。 我的想法是在模型中定義靜態變量,這些靜態變量不是在數據庫中創建的,而只對生成過程很重要。 例如,我只想顯示show.gsp中的一些特殊字段,但是我想顯示_form.gsp中的每個字段或者我想要執行一些gsp導入,但只能在單獨的gsp中執行。 所以我認爲我可以定義一些靜態變量,其中的值包含一些可以在生成模板中解釋的配置參數。 我希望大家明白我的意思嗎?grails scaffolding由domainclass配置

下面是一個例子:

class Awesome { 
Long awesome_level 
Long person_name 
    Boolean itsMe 

static String showFields 
static transients = ['showFields'] 

static constraints = { 
    einrichtungs_type() 
    type_of_conzept() 
    anzahl_gruppen() 
    anzahl_kinder_pro_Gruppe() 
    offnungszeiten() 
    showFields(["person_name", "itsMe"]) 
} 

在Show-查看我只想顯示在陣列「showFields」領域

... 
for (p in props) { 
    if (p.embedded && p.name in domainClass.showFields) { 
     def embeddedPropNames = p.component.persistentProperties*.name 
     def embeddedProps = p.component.properties.findAll { embeddedPropNames.contains(it.name) && !excludedProps.contains(it.name) } 
     Collections.sort(embeddedProps, comparator.constructors[0].newInstance([p.component] as Object[])) 
     %><fieldset class="embedded"><legend><g:message code="${domainClass.propertyName}.${p.name}.label" default="${p.naturalName}" /></legend><% 
      for (ep in p.component.properties) { 
       renderFieldForProperty(ep, p.component, "${p.name}.") 
      } 
     %></fieldset><% 

    } else { 
     renderFieldForProperty(p, domainClass) 
    } 
... 

我知道,如果條款不工作。我的問題是,我無法獲得字段「showFields」的價值。 知道我的問題:

  • 它能夠接收域類的字段的值嗎?
  • 它能夠執行一個domainclass的方法嗎?
  • 有沒有其他的方法可以定義配置參數,我可以在生成模板中訪問?

我希望我能夠顯示我的問題,並感謝您的幫助! Greetz V

回答

0

我找到了解決方案。首先,我認爲創建一個自定義約束條件是可能的。我仍然認爲這也是可能的,但我發現一個更好的/ easyer的方式來添加「配置」。 我使用已存在的標籤屬性。如果我理解正確的話,我用來在select-Html-tags中添加屬性的屬性參數。現在我用它來添加一些配置參數。在這裏我的解決辦法:

  1. 你必須改變「renderEditor.template」不是所有的配置參數將在HTML的標記作爲屬性添加。 cp.attributes.each { k, v -> sb << "${k}=\"${v}\" " }變化cp.attributes?.realAttributes.each { k, v -> sb << "${k}=\"${v}\" " }
  2. 如果你真的想這個屬性的功能添加到屬性參數「realAttributes」(或者你想怎麼稱呼它)
  3. 改變模型的約束值:在(我例如:)

靜態約束= {einrichtungs_type(屬性: [showField:TRUE]) }

  1. Finaly改變一代模板

如果(cp.attributes?.showField){ ...

我希望這4個步驟可以幫助你,如果你有幾乎相同的問題。

格爾茨

V