开发者

Associations and the Grails webflow

it's my first time using webflows in Grails and I can't seem to solve this.

I have 3 domain classes with associations that look something like this:

class A {
  ...
  static hasMany = [ b : B ]
  ...
}

class B {
  ...
 开发者_如何学编程 static belongsTo = [ a : A ]
  static hasMany = [ c : C ]
  ...
}

class C {
  ...
  static belongsTo = [ b : B ]
  ...
}

Now, the GSP communicates with the controller via Javascript (due to my use of Dojo). When I try to remoteFunction a normal action, I can do something like this:

def action1 = {
   def anId = params.id
   def currA = A.get(anId)
   def sample = currA.b?.c // I can get all the way to 'c' without any problems
   ...
}

However, I have a webflow and the contents of that action is in the webflow... It looks something like this:

def someFlow = {
   ...
   someState {
      on("next") {
         def anId = params.id // this does NOT return a null value
         def currA = A.get(anId) // this does NOT return a null value
         def sample = currA.b // error already occurs here and I need to get 'c'!
      }.to("somePage")
      ...
   }
   ...
}

In this case, it tells me that b doesn't exist... so I can't even get to 'c'. Any suggestions on what to do??? Thanks... getting real desperate...


Hmmm, not quite sure what the problem is, but I do find something curious. In your first block, you use the following:

 def sample = currA.b?.c

According to the classes you've created, currA.b is a Collection of Bs, not a single one. Therefore currA.b?.c would be a Collection of Collections of C class instances, one Collection for each of the Bs in currA.b

I'm not sure what would happen if currA.b was empty... given the safety operator, which equates empty with null (via GroovyTruth), I'd say sample would be null.

None of this helps determine why the line you specify generates an error, though. Perhaps you can show us what the error is? Is it an NPE or some other?

Looking at this more, It also looks like you're missing the "to" function call:

on("event") { intraEvent code }.**to** "eventHandlerAction"

Perhaps the error you're seeing is the result of this?

Seems not.

Ok, only other thing I can think of is that some time ago (v1.1 I think), they made it so you have to use "this." a lot more inside flow actions. Essentially, in order to access controller class-level objects and methods, you need to put "this." in front of the access to them. Say you wanted to put that intraEvent code you've got into a method:

def goGetC() {      
   def anId = params.id 
   def currA = A.get(anId) 
   def sample = currA.b 
}

In order to call that method from your event code, you'd have to use:

   someState {
      on("next") {
         this.goGetC()
      }.to ("wherever")
   }

If you tried to call goGetC() without the "this.", you'd instead be ending your eventhandler with the event "goGetC". I'm not sure if your simplification of your real code is hiding a similar case or not, and I'm still not sure what error exactly you're getting, but this was something that bit me a while ago and it's Flow specific. Hope this helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜