Search code examples
grailszk

Access grails zkui composer instance of dynamically included view from its parent


I really could use some help here. The scenario is that I am including views within my webapp dynamically depending on user interaction. And the main problem is that i need to access the current instance of the included views composer.

When the user has made some selection and a sub-view is loaded, the user can enter values in the sub-view which i then need to collect to the main view.

Some code fragments that shows somehow what i'm trying to accomplish: (only necessary parts)

grails-app/views/mainview/main.gsp
    ...
    <z:div id="mainContent"></z:div>
    ...

grails-app/views/mainview/templates/one.gsp
    <z:window apply="org.package.composers.OneComposer">
        ...
    </z:window>

grails-app/views/mainview/templates/two.gsp
    <z:window apply="org.package.composers.TwoComposer">
        ...
    </z:window>

grails-app/composers/org/packages/composers/MainComposer.groovy
    Div mainContent
    Composer currentSubComposer = null
    ...
    void setSubContent(String templateURI){
        mainContent.children.clear()
        mainContent << { include(src: templateURI) }
        currentSubComposer = /* HOW TO GET THE COMPOSER INSTANCE ? */
    }
    ...

I know that if you include a sub-view "statically" in your main view without using dynamically using include, Then you can access the sub-composer from the parent as the following code fragment shows:

grails-app/views/common/_subview.gsp
    <z:window id="subview" apply="org.package.composers.SubviewComposer">

grails-app/views/mainview/main.gsp
    ...
    <z:div id="mainContent">
        <g:render template="/common/subview" />
    </z:div>
    ...

grails-app/composers/org/packages/composers/MainComposer.groovy
    Window subview
    Composer c = (Composer)subview.getAttribute("subview#composer")

But i cannot do this as everything is dynamic. I need to be able to do the same thing somehow.

So to break down the basic question:

  • I have a main composer
  • In main composer i include a gsp file (sub view)
  • The sub view has its own composer

How do I access the sub view composer instance from the main composer?

Any help appreciated!


Solution

  • I'll post my own solution to this, using the grails session, if anyone else is interested. Perhaps there exists other ways of solving this but does the trick:

    I created a callback/register method in main composer:

    public void registerChildComposer(Composer c){
        currentChildComposer = c
    }
    

    and before the sub-view is included in the main composer:

    session["parentComposerInstance"] = this
    

    and in doAfterCompose of sub-view composer:

    Composer parentComposer = session["parentComposerInstance"]
    parentComposer.registerChildComposer(this)
    

    and voila, after this "handshake"-like pattern i got the handle to the sub view composer i was looking for.