开发者

Grails efficient hasMany-Relationship in View

I'm saving contacts (email, mobile phone, ICQ, AIM etc.) for people like this:

class Person {
  static hasMany = {
    conta开发者_JAVA技巧cts: Contact
  }
}
class Contact {
  String code
  ContactType type
}
class ContactType {
  String name
}

In my view, I've written some Templates for displaying each contact with a select-box for the contact-type and a textfield for the code, spiced up with some JavaScript for adding and deleting.

My question is: Is there an easy and elegant way to update the data similar to personInstance.properties = params or do I have to read out all the fields, deleting removed, updating changed and adding new ones?


I was looking into this some time ago but never got to refactor our code which handles parameters the old-fashioned way.

According to http://www.grails.org/Controllers+-+Data+Binding you can do something like this

def person = new Person(params['person'])
def contact = new Contact(params['contact'])
def conctactType = new ContactType(params['contactType'])

as long as request params are properly namespaced

person.contact.code
person.contact.type.name

You would still have to find out how to handle one to many. Maybe someone who knows can chip in.

Edit:

Came across this doc which describes how to handle one-to-many. It doesn't appear on the main grails site:

http://svn.codehaus.org/grails/tags/GRAILS_DOCS_1_1/src/guide/6.1.6%20Data%20Binding.gdoc

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜