简体   繁体   English

Grails:了解groovy DomainClass.properties

[英]Grails: Understanding groovy DomainClass.properties

I'm having a hard time finding information about grails functionality: 我很难找到有关grails功能的信息:

DomainClass.properties = params

In my particular case, I have these classes: 在我的特定情况下,我有这些类:

class parameterType = {
String name
String desc
static hasMany = [codes : parameterCode]
...
}

class parameterCode = {
String code
String desc
static belongsTo = [parameterType : parameterType]
}

My parameterType/edit.gsp has name, desc and an html table with its list of parameterCodes 我的parameterType / edit.gsp有name,desc和一个带有parameterCodes列表的html表

At first, I had some variation of the scaffolded controller on the 'update' action. 起初,我在“更新”操作中有一些脚手架控制器的变化。 That (I know its wrong but it was a beginners code) it first deleted all the parameterCodes and then reassociated them (or recreated them). 那(我知道它错了,但它是初学者代码)它首先删除了所有的parameterCodes,然后重新关联它们(或重新创建它们)。

With Ajax I was sending the data in this format: 使用Ajax,我以这种格式发送数据:

id=1234&name=paramName&desc=paramDesc&codes[0].code=code1&codes[0].desc=codeDesc1&codes[1].code=code2&codes[1].desc=codeDesc2

And in the controller I had this: 在控制器中我有这个:

def parameterTypeInstance = ParameterType.get(params.id)
def toDelete = parameterTypeInstance.parameterCodes

parameterTypeInstance.parameterCodes = []
toDelete.each{it.delete(flush: true)}

//And this "magic" line reassociated all the properties in parameterType And Created his parameterCodes in the data base:
parameterTypeInstance.properties = params

I honestly don't how it works, and I just wanted to know if there's a way of doing the same thing without having to previously delete the associated parameterCodes. 老实说,我不知道它是如何工作的,我只是想知道是否有办法做同样的事情而不必事先删除相关的parameterCodes。

Cheers 干杯

**Update:** **更新:**

I just found what I was looking for in these links: 我刚刚在这些链接中找到了我要找的东西:
http://www.2paths.com/2009/10/01/one-to-many-relationships-in-grails-forms/ http://omarello.com/2010/08/grails-one-to-many-dynamic-forms/ http://www.2paths.com/2009/10/01/one-to-many-relationships-in-grails-forms/ http://omarello.com/2010/08/grails-one-to-many-动态形式/

But I had another error. 但我有另一个错误。

These talks about LazyList and decorate(), so I just added the next lines to my ParameterType Class: 这些讨论了LazyList和decorate(),所以我只是将下一行添加到我的ParameterType类中:

def List getExpandableCodeList() {
    return LazyList.decorate(codes,FactoryUtils.instantiateFactory(ParameterCode.class))
}

But when I do this in my controller update: 但是当我在控制器更新中执行此操作时:

parameterTypeInstance.properties = params

I'm getting this error: 我收到这个错误:

groovy.lang.MissingMethodException: No signature of method: static org.apache.commons.collections.list.LazyList.decorate() is applicable for argument types: (org.hibernate.collection.PersistentSet, org.apache.commons.collections.functors.InstantiateFactory) values: [[cE - EE, cA - AA, cC - CC, cD - DD], org.apache.commons.collections.functors.InstantiateFactory@dd768d] groovy.lang.MissingMethodException:没有方法签名:static org.apache.commons.collections.list.LazyList.decorate()适用于参数类型:(org.hibernate.collection.PersistentSet,org.apache.commons.collections。 functors.InstantiateFactory)值:[[cE - EE,cA - AA,cC - CC,cD - DD],org.apache.commons.collections.functors.InstantiateFactory@dd768d]

The data is being recieved in the controller this way: 数据以这种方式在控制器中接收:

expandableCodeList[0].desc: AA expandableCodeList [0] .desc:AA
expandableCodeList[3].code: cE expandableCodeList [3] .code:cE
expandableCodeList[3].id: 35073 expandableCodeList [3] .id:35073
expandableCodeList[1].id: 35076 expandableCodeList [1] .id:35076
expandableCodeList[0].code: cA expandableCodeList [0] .code:cA
expandableCodeList[2].code: cD expandableCodeList [2] .code:cD
expandableCodeList[1].desc: CC expandableCodeList [1] .desc:CC
expandableCodeList[0].id: 35080 expandableCodeList [0] .id:35080
expandableCodeList[3].desc: EE expandableCodeList [3] .desc:EE
expandableCodeList[2].id: 35075 expandableCodeList [2] .id:35075

Any hints on what I'm doing wrong? 关于我做错了什么的暗示? should I be sending the data in another format? 我应该以其他格式发送数据吗?

Any help would be much appreciated. 任何帮助将非常感激。 Thanks. 谢谢。

If I not in error .properties is a method added by groovy to the java.lang.Object , look to this javaodc 如果我没有错误.properties是一个由groovy添加到java.lang.Object的方法,请查看此javaodc

There is many way to do what you want to do. 有很多方法可以做你想做的事。 Please look to grails documentation on data binding . 请查看有关数据绑定的 grails文档。

For example you can do 例如,你可以做到

bindData(parameterTypeInstance, params, [exclude: parameterTypeInstance.parameterCodes])

look here for more info on bindData 在这里查看有关bindData的更多信息

Ok, I'm settings this as an answer: 好的,我把它设置为答案:

http://www.2paths.com/2009/10/01/one-to-many-relationships-in-grails-forms/ http://www.2paths.com/2009/10/01/one-to-many-relationships-in-grails-forms/

At last, lazylist and the decorate() method was what I was looking for. 最后,lazylist和decorate()方法是我正在寻找的。 Sadly it takes to take the child collection as a list and it carries out a lot of other issues for me. 遗憾的是,将儿童收藏作为一个清单,它会为我带来许多其他问题。 But its a good solution if anyone needs to make a view with the parent and its child objects the "simple" way. 但是,如果有人需要使用父项及其子对象以“简单”方式进行查看,那么这是一个很好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM