You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by gmparker2000 <gr...@brovada.com> on 2012/05/30 22:22:19 UTC

Access to Page from LoadableDetachableModel

I have a situation where I need to access the page from the load method of a
loadableDetachableModel.  The loadableDetachableModel is used deep down on a
panel that needs access to the page in order to access the model object.  To
complicate things this page is abstract and can be extended to provide
access to the model differently (i.e. from a database, or from a file).  It
kind of looks like this (only more complicated in my case):

/** Specialization of MyPage that gets the model from a database. */
public class MyRDBMSPage extends MyPage {
    public MyModel getMyModel() {
        // go get the model from the database
    }
    ....
}

/** Specialization of MyPage that gets the model from a file. */
public class MyFilePage extends MyPage {
    public MyModel getMyModel() {
        // go get the model from the filesystem
    }
    ....
}

/** Abstract MyPage that doesn't care where the model came from (database or
file). */
public abstract class MyPage extends WebPage {
    public abstract MyModel getMyModel();

    public onInitialize() {
        this.add(new MyPanel("myPanel", this.getMyModel()));
        ....
    }
}

/** Panel that has the detachable model. */
public class MyPanel {
    public MyPanel(String id, MyModel model) {
        MyDetachableModel mdl = new MyDetachableModel(model);
        this.add(new CheckBox("myCheckbox", new PropertyModel(mdl,
"isSelected"));
        ....
    }
}

/** Detachable model that needs access to the page. */
public class MyDetachableModel extends LoadableDetachableModel<MyModel> {
        private transient MyModel myModel;

        public MyDetachableModel(MyModel model) {
            // maybe store something from the model to help me retrieve the 
            // model in the load method????

            // I could store a transient reference but that is no good
beyond the initial page
            // render
            this.myModel = model;
        }

        protected MyModel load() {
                // if I had access to the page I could get the model from
there but
                // it doesn't appear to be available at the point when load
is called.
                return [GET MY PAGE HERE SOMEHOW].getMyModel();
        }
}

It would be great to be able to do this so that different variations on a
single page could be created easily.  Perhaps I am overlooking something or
over complicating this.  We are also using spring so it is possible to
inject (@SpringBean) a bean into the detachable model that provides access
to the model but I'd rather avoid this as I think it complicates something
that should be quite simple.

Maybe its just me but I'm finding loadableDetachableModel to be quite
challenging for my purposes, which are not typically just loading database
entities.  Serializing a key piece of information instead of the model makes
sense and seems rather straightforward but I struggle not with the stuff
getting serialized but rather the object that is needed to operate on the
key information to get the model back (i.e a business object, entity
manager, etc).

If anyone can show me where I'm going wrong I'd appreciate the help

thanks

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Access-to-Page-from-LoadableDetachableModel-tp4649586.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Access to Page from LoadableDetachableModel

Posted by gmparker2000 <gr...@brovada.com>.
The reason for the detachable stuff is that when the page gets serialized it
is going to serialize all the components in the page hierarchy and the
models bound to them.  When that happens MyPOJO is going also get serialized
which I'm trying to avoid.  In my application these objects can be very
large and I don't want them serialized to the session.

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Access-to-Page-from-LoadableDetachableModel-tp4649586p4649596.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Access to Page from LoadableDetachableModel

Posted by Sven Meier <sv...@meiers.net>.
Ok, MyPOJO makes more sense.

But then again, why don't you just use a model:

     public abstract class MyPage extends WebPage {
         public abstract MyPOJO getMyPOJO();
         public onInitialize() {
             this.add(new MyPanel("myPanel", new 
AbstractReadOnlyModel<MyPOJO>() {
                 public MyPOJO getObject() {
                     return getMyPOJO();
                 }
             }));
         }
     }

No need for any detachable stuff.

Sven

On 05/30/2012 10:44 PM, gmparker2000 wrote:
> My quick example isn't real and perhaps a bit misleading.  Actually there is
> nothing special about MyModel, its just a POJO that I am wrapping up in a
> detachable model.  Maybe I misunderstood what you were trying to tell me.
>
> /** Specialization of MyPage that gets the POJO from a database. */
> public class MyRDBMSPage extends MyPage {
>      public MyPOJO getMyPOJO() {
>          // go get the POJO from the database
>      }
>      ....
> }
>
> /** Specialization of MyPage that gets the POJO from a file. */
> public class MyFilePage extends MyPage {
>      public MyPOJO getMyPOJO() {
>          // go get the POJO from the filesystem
>      }
>      ....
> }
>
> /** Abstract MyPage that doesn't care where the POJO came from (database or
> file). */
> public abstract class MyPage extends WebPage {
>      public abstract MyPOJO getMyPOJO();
>
>      public onInitialize() {
>          this.add(new MyPanel("myPanel", this.getMyPOJO()));
>          ....
>      }
> }
>
> /** Panel that has the detachable model. */
> public class MyPanel {
>      public MyPanel(String id, MyPOJO pojo) {
>          MyDetachableModel mdl = new MyDetachableModel(pojo);
>          this.add(new CheckBox("myCheckbox", new PropertyModel(mdl,
> "isSelected"));
>          ....
>      }
> }
>
> /** Detachable model that needs access to the page. */
> public class MyDetachableModel extends LoadableDetachableModel<MyPOJO>  {
>          private transient MyPOJO myPOJO;
>
>          public MyDetachableModel(MyModel pojo) {
>              // maybe store something from the POJO to help me retrieve the
>              // POJO in the load method????
>
>              // I could store a transient reference but that is no good
> beyond the initial page
>              // render
>              this.myPOJO = pojo;
>          }
>
>          protected MyPOJO load() {
>                  // if I had access to the page I could get the pojo from
> there but
>                  // it doesn't appear to be available at the point when load
> is called.
>                  return [GET MY PAGE HERE SOMEHOW].getMyPOJO();
>          }
> }
>
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/Access-to-Page-from-LoadableDetachableModel-tp4649586p4649590.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Access to Page from LoadableDetachableModel

Posted by gmparker2000 <gr...@brovada.com>.
My quick example isn't real and perhaps a bit misleading.  Actually there is
nothing special about MyModel, its just a POJO that I am wrapping up in a
detachable model.  Maybe I misunderstood what you were trying to tell me.

/** Specialization of MyPage that gets the POJO from a database. */
public class MyRDBMSPage extends MyPage {
    public MyPOJO getMyPOJO() {
        // go get the POJO from the database
    }
    ....
}

/** Specialization of MyPage that gets the POJO from a file. */
public class MyFilePage extends MyPage {
    public MyPOJO getMyPOJO() {
        // go get the POJO from the filesystem
    }
    ....
}

/** Abstract MyPage that doesn't care where the POJO came from (database or
file). */
public abstract class MyPage extends WebPage {
    public abstract MyPOJO getMyPOJO();

    public onInitialize() {
        this.add(new MyPanel("myPanel", this.getMyPOJO()));
        ....
    }
}

/** Panel that has the detachable model. */
public class MyPanel {
    public MyPanel(String id, MyPOJO pojo) {
        MyDetachableModel mdl = new MyDetachableModel(pojo);
        this.add(new CheckBox("myCheckbox", new PropertyModel(mdl,
"isSelected"));
        ....
    }
}

/** Detachable model that needs access to the page. */
public class MyDetachableModel extends LoadableDetachableModel<MyPOJO> {
        private transient MyPOJO myPOJO;

        public MyDetachableModel(MyModel pojo) {
            // maybe store something from the POJO to help me retrieve the
            // POJO in the load method????

            // I could store a transient reference but that is no good
beyond the initial page
            // render
            this.myPOJO = pojo;
        }

        protected MyPOJO load() {
                // if I had access to the page I could get the pojo from
there but
                // it doesn't appear to be available at the point when load
is called.
                return [GET MY PAGE HERE SOMEHOW].getMyPOJO();
        }
} 

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Access-to-Page-from-LoadableDetachableModel-tp4649586p4649590.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Access to Page from LoadableDetachableModel

Posted by Sebastien <se...@gmail.com>.
Hi,

I added the panel in the onInitialize method to keep the way you did and
because I don't know the rest of the code. But, for the specific part of
code we exchanged, the panel could be added in the constructor (or best, in
an init() method called by the constructor to be clean). There is also no
problem to have the LDM as a nested class; for the quick start, the LDM was
in the MyPanel.java (not nested then, but there is no huge difference
strictly java speaking).

Regards,
Sebastien.

On Thu, May 31, 2012 at 1:41 AM, gmparker2000 <gr...@brovada.com>wrote:

> Thank you so much Sebastien and Sven!  Sebastien I altered my actual code
> to
> put the panel on the LDM and it didn't work on the initial try.  The
> problem
> with the illegal state exception I was having seems to happen if you
> construct your components in the panel constructor instead of the
> onInitialize method.  Once I made sure everything was using the
> onInitialize
> method the panel.getPage() method started working presumably because the
> onInitialize of the panel triggers the LDM load after the panel is
> officially added to the page.
>
> Next I'm going to try to phase out the panel member variable on the LDM by
> using MyPanel.this.getPage() instead.  This should work and then I can be
> sure that I'm not serializing the panel with the LDM.
>

Re: Access to Page from LoadableDetachableModel

Posted by gmparker2000 <gr...@brovada.com>.
Update - I forgot to mention that my LDM is a class nested in the panel
class.

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Access-to-Page-from-LoadableDetachableModel-tp4649586p4649603.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Access to Page from LoadableDetachableModel

Posted by gmparker2000 <gr...@brovada.com>.
Update - Access the page with MyPanel.this.getPage() seems to work fine. 
More testing is required but looks like I have a solution.

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Access-to-Page-from-LoadableDetachableModel-tp4649586p4649602.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Access to Page from LoadableDetachableModel

Posted by gmparker2000 <gr...@brovada.com>.
Thank you so much Sebastien and Sven!  Sebastien I altered my actual code to
put the panel on the LDM and it didn't work on the initial try.  The problem
with the illegal state exception I was having seems to happen if you
construct your components in the panel constructor instead of the
onInitialize method.  Once I made sure everything was using the onInitialize
method the panel.getPage() method started working presumably because the
onInitialize of the panel triggers the LDM load after the panel is
officially added to the page.

Next I'm going to try to phase out the panel member variable on the LDM by
using MyPanel.this.getPage() instead.  This should work and then I can be
sure that I'm not serializing the panel with the LDM.

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Access-to-Page-from-LoadableDetachableModel-tp4649586p4649601.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Access to Page from LoadableDetachableModel

Posted by Sebastien <se...@gmail.com>.
Hello again,

I tested in a quickstart and it works. (I just noticed you did not called
super.onInitialize())

The code I used:

class MyPOJO
{
    public Boolean isSelected()
    {
        return true;
    }
}

public class HomePage extends WebPage
{
    private static final long serialVersionUID = 1L;

    public MyPOJO getMyPOJO()
    {
        return new MyPOJO();
    }

    public void onInitialize()
    {
        super.onInitialize();

        this.add(new MyPanel("myPanel", this.getMyPOJO()));
    }
}


class MyPanel extends Panel
{
    private static final long serialVersionUID = 1L;

    public MyPanel(String id, MyPOJO pojo)
    {
        super(id);

        MyDetachableModel mdl = new MyDetachableModel(this);
        this.add(new CheckBox("myCheckbox", new PropertyModel<Boolean>(mdl,
"selected")));
    }
}

class MyDetachableModel extends LoadableDetachableModel<MyPOJO>
{
    private static final long serialVersionUID = 1L;
    private MyPanel myPanel;

    public MyDetachableModel(MyPanel panel)
    {
        this.myPanel = panel;
    }

    protected MyPOJO load()
    {
        HomePage page = (HomePage) myPanel.getPage();

        MyPOJO pojo = page.getMyPOJO();
        System.out.println(pojo.isSelected());

        return pojo;
    }
}

Re: Access to Page from LoadableDetachableModel

Posted by gmparker2000 <gr...@brovada.com>.
Well I gave this approach a try but unfortunately the first time the load
method gets called the getPage method throws an illegal state exception

java.lang.IllegalStateException: No Page found for component

So I guess this means that during the construction of the panel the load is
getting called before the panel is actually added to the page:


public abstract class MyPage extends WebPage {
    public abstract MyPOJO getMyPOJO();

    public onInitialize() {
        this.add(
               */** During the construction of the panel the load method of
the loadable detachable model
                     is called but as you can see this panel has not been
added to the page yet */*
               new MyPanel("myPanel", this.getMyPOJO())
        );
        ....
    }
}

public class MyPanel {
    public MyPanel(String id, MyPOJO pojo) {
        MyDetachableModel mdl = new MyDetachableModel(this);
        this.add(new CheckBox("myCheckbox", new PropertyModel(mdl,
"isSelected"));
        ....
    }
}

/** Detachable model that needs access to the page. */
public class MyDetachableModel extends LoadableDetachableModel<MyPOJO> {
        private MyPanel myPanel;

        public MyDetachableModel(MyPanel panel) {
            this.myPanel = panel;
        }

        protected MyPOJO load() {
           *// this throws illegalstate exception because the panel is added
to the page yet.*
           MyPage page = (MyPage) myPanel.getPage();

           return page.getMyPOJO();
        }
} 

The only other way I can think to do this is to also pass MyPOJO to the
MyDetachableModel constructor to get the initial model and store it in a
transient member variable.  When load gets called the second time the panel
is now on the page and could be used to get MyPOJO.  This might work but its
getting somewhat ugly.  There must be a better way.



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Access-to-Page-from-LoadableDetachableModel-tp4649586p4649597.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Access to Page from LoadableDetachableModel

Posted by Sebastien <se...@gmail.com>.
Thanks for the update!

Re: Access to Page from LoadableDetachableModel

Posted by Sven Meier <sv...@meiers.net>.
A model can have a reference to a component, that's no problem for 
serialization.

Sven

On 05/30/2012 11:17 PM, Sebastien wrote:
> Well, I don't think so, even it needs to be tested.
>
> My guess is that the panel is added to the page (then, serialized). To the
> LDM's contructor, you will pass 'this' (means, the panel). In the LDM, you
> will store the reference of that 'this' into a variable. In all case, we
> always have the reference to the same object. So I would say (but I also
> would have tested) that the panel will be serialized only once...
>
> Maybe someone else can confirm/disaffirm..
>
>
> On Wed, May 30, 2012 at 10:48 PM, gmparker2000<gr...@brovada.com>wrote:
>
>> But if I do that won't the panel get serialized with the loadable
>> detachable
>> model?  I would have to store a reference on the detachable model in order
>> to use it in the load method.  I could mark it transient and it would be
>> there when the page is initially rendered but not on subsequent requests.
>>
>> This would be exactly what I'm looking for but I don't want to
>> inadvertently
>> serialize the panel.  Or does it matter?
>>
>> --
>> View this message in context:
>> http://apache-wicket.1842946.n4.nabble.com/Access-to-Page-from-LoadableDetachableModel-tp4649586p4649591.html
>> Sent from the Users forum mailing list archive at Nabble.com.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Access to Page from LoadableDetachableModel

Posted by Sebastien <se...@gmail.com>.
Well, I don't think so, even it needs to be tested.

My guess is that the panel is added to the page (then, serialized). To the
LDM's contructor, you will pass 'this' (means, the panel). In the LDM, you
will store the reference of that 'this' into a variable. In all case, we
always have the reference to the same object. So I would say (but I also
would have tested) that the panel will be serialized only once...

Maybe someone else can confirm/disaffirm..


On Wed, May 30, 2012 at 10:48 PM, gmparker2000 <gr...@brovada.com>wrote:

> But if I do that won't the panel get serialized with the loadable
> detachable
> model?  I would have to store a reference on the detachable model in order
> to use it in the load method.  I could mark it transient and it would be
> there when the page is initially rendered but not on subsequent requests.
>
> This would be exactly what I'm looking for but I don't want to
> inadvertently
> serialize the panel.  Or does it matter?
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/Access-to-Page-from-LoadableDetachableModel-tp4649586p4649591.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: Access to Page from LoadableDetachableModel

Posted by gmparker2000 <gr...@brovada.com>.
But if I do that won't the panel get serialized with the loadable detachable
model?  I would have to store a reference on the detachable model in order
to use it in the load method.  I could mark it transient and it would be
there when the page is initially rendered but not on subsequent requests.

This would be exactly what I'm looking for but I don't want to inadvertently
serialize the panel.  Or does it matter?

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Access-to-Page-from-LoadableDetachableModel-tp4649586p4649591.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Access to Page from LoadableDetachableModel

Posted by Sebastien <se...@gmail.com>.
Hi,

I aggree with Sven. Another option is to pass the panel to the LDM's
contructor so you can do a panel.getPage() to get the page. Just cast to
the appropriate page type and do yourpage.getMyModel().

Best regards,
Sebastien.


On Wed, May 30, 2012 at 10:32 PM, Sven Meier <sv...@meiers.net> wrote:

> > public MyPanel(String id, MyModel model) {
>
> That looks suspicious, why should MyPanel require a specific model
> implementation?
>
> What's so special about your MyModel? Is it a Wicket model, i.e.
> implements IModel?
> If yes, then the constructor should look like this:
>
>  public MyPanel(String id, IModel<Foo> model) {
>
> If not, the following would be my suggestion:
>
>  public MyPanel(String id, IModel<MyModel> model) {
>
> Hope this helps
> Sven
>
>
> On 05/30/2012 10:22 PM, gmparker2000 wrote:
>
>> I have a situation where I need to access the page from the load method
>> of a
>> loadableDetachableModel.  The loadableDetachableModel is used deep down
>> on a
>> panel that needs access to the page in order to access the model object.
>>  To
>> complicate things this page is abstract and can be extended to provide
>> access to the model differently (i.e. from a database, or from a file).
>>  It
>> kind of looks like this (only more complicated in my case):
>>
>> /** Specialization of MyPage that gets the model from a database. */
>> public class MyRDBMSPage extends MyPage {
>>     public MyModel getMyModel() {
>>         // go get the model from the database
>>     }
>>     ....
>> }
>>
>> /** Specialization of MyPage that gets the model from a file. */
>> public class MyFilePage extends MyPage {
>>     public MyModel getMyModel() {
>>         // go get the model from the filesystem
>>     }
>>     ....
>> }
>>
>> /** Abstract MyPage that doesn't care where the model came from (database
>> or
>> file). */
>> public abstract class MyPage extends WebPage {
>>     public abstract MyModel getMyModel();
>>
>>     public onInitialize() {
>>         this.add(new MyPanel("myPanel", this.getMyModel()));
>>         ....
>>     }
>> }
>>
>> /** Panel that has the detachable model. */
>> public class MyPanel {
>>     public MyPanel(String id, MyModel model) {
>>         MyDetachableModel mdl = new MyDetachableModel(model);
>>         this.add(new CheckBox("myCheckbox", new PropertyModel(mdl,
>> "isSelected"));
>>         ....
>>     }
>> }
>>
>> /** Detachable model that needs access to the page. */
>> public class MyDetachableModel extends LoadableDetachableModel<**MyModel>
>>  {
>>         private transient MyModel myModel;
>>
>>         public MyDetachableModel(MyModel model) {
>>             // maybe store something from the model to help me retrieve
>> the
>>             // model in the load method????
>>
>>             // I could store a transient reference but that is no good
>> beyond the initial page
>>             // render
>>             this.myModel = model;
>>         }
>>
>>         protected MyModel load() {
>>                 // if I had access to the page I could get the model from
>> there but
>>                 // it doesn't appear to be available at the point when
>> load
>> is called.
>>                 return [GET MY PAGE HERE SOMEHOW].getMyModel();
>>         }
>> }
>>
>> It would be great to be able to do this so that different variations on a
>> single page could be created easily.  Perhaps I am overlooking something
>> or
>> over complicating this.  We are also using spring so it is possible to
>> inject (@SpringBean) a bean into the detachable model that provides access
>> to the model but I'd rather avoid this as I think it complicates something
>> that should be quite simple.
>>
>> Maybe its just me but I'm finding loadableDetachableModel to be quite
>> challenging for my purposes, which are not typically just loading database
>> entities.  Serializing a key piece of information instead of the model
>> makes
>> sense and seems rather straightforward but I struggle not with the stuff
>> getting serialized but rather the object that is needed to operate on the
>> key information to get the model back (i.e a business object, entity
>> manager, etc).
>>
>> If anyone can show me where I'm going wrong I'd appreciate the help
>>
>> thanks
>>
>> --
>> View this message in context: http://apache-wicket.1842946.**
>> n4.nabble.com/Access-to-Page-**from-LoadableDetachableModel-**
>> tp4649586.html<http://apache-wicket.1842946.n4.nabble.com/Access-to-Page-from-LoadableDetachableModel-tp4649586.html>
>> Sent from the Users forum mailing list archive at Nabble.com.
>>
>> ------------------------------**------------------------------**---------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.**apache.org<us...@wicket.apache.org>
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: users-unsubscribe@wicket.**apache.org<us...@wicket.apache.org>
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: Access to Page from LoadableDetachableModel

Posted by Sven Meier <sv...@meiers.net>.
 > public MyPanel(String id, MyModel model) {

That looks suspicious, why should MyPanel require a specific model 
implementation?

What's so special about your MyModel? Is it a Wicket model, i.e. 
implements IModel?
If yes, then the constructor should look like this:

   public MyPanel(String id, IModel<Foo> model) {

If not, the following would be my suggestion:

   public MyPanel(String id, IModel<MyModel> model) {

Hope this helps
Sven

On 05/30/2012 10:22 PM, gmparker2000 wrote:
> I have a situation where I need to access the page from the load method of a
> loadableDetachableModel.  The loadableDetachableModel is used deep down on a
> panel that needs access to the page in order to access the model object.  To
> complicate things this page is abstract and can be extended to provide
> access to the model differently (i.e. from a database, or from a file).  It
> kind of looks like this (only more complicated in my case):
>
> /** Specialization of MyPage that gets the model from a database. */
> public class MyRDBMSPage extends MyPage {
>      public MyModel getMyModel() {
>          // go get the model from the database
>      }
>      ....
> }
>
> /** Specialization of MyPage that gets the model from a file. */
> public class MyFilePage extends MyPage {
>      public MyModel getMyModel() {
>          // go get the model from the filesystem
>      }
>      ....
> }
>
> /** Abstract MyPage that doesn't care where the model came from (database or
> file). */
> public abstract class MyPage extends WebPage {
>      public abstract MyModel getMyModel();
>
>      public onInitialize() {
>          this.add(new MyPanel("myPanel", this.getMyModel()));
>          ....
>      }
> }
>
> /** Panel that has the detachable model. */
> public class MyPanel {
>      public MyPanel(String id, MyModel model) {
>          MyDetachableModel mdl = new MyDetachableModel(model);
>          this.add(new CheckBox("myCheckbox", new PropertyModel(mdl,
> "isSelected"));
>          ....
>      }
> }
>
> /** Detachable model that needs access to the page. */
> public class MyDetachableModel extends LoadableDetachableModel<MyModel>  {
>          private transient MyModel myModel;
>
>          public MyDetachableModel(MyModel model) {
>              // maybe store something from the model to help me retrieve the
>              // model in the load method????
>
>              // I could store a transient reference but that is no good
> beyond the initial page
>              // render
>              this.myModel = model;
>          }
>
>          protected MyModel load() {
>                  // if I had access to the page I could get the model from
> there but
>                  // it doesn't appear to be available at the point when load
> is called.
>                  return [GET MY PAGE HERE SOMEHOW].getMyModel();
>          }
> }
>
> It would be great to be able to do this so that different variations on a
> single page could be created easily.  Perhaps I am overlooking something or
> over complicating this.  We are also using spring so it is possible to
> inject (@SpringBean) a bean into the detachable model that provides access
> to the model but I'd rather avoid this as I think it complicates something
> that should be quite simple.
>
> Maybe its just me but I'm finding loadableDetachableModel to be quite
> challenging for my purposes, which are not typically just loading database
> entities.  Serializing a key piece of information instead of the model makes
> sense and seems rather straightforward but I struggle not with the stuff
> getting serialized but rather the object that is needed to operate on the
> key information to get the model back (i.e a business object, entity
> manager, etc).
>
> If anyone can show me where I'm going wrong I'd appreciate the help
>
> thanks
>
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/Access-to-Page-from-LoadableDetachableModel-tp4649586.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org