You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Per Newgro <pe...@gmx.ch> on 2007/10/12 10:56:41 UTC

HOWTO - update models in chain (push or pull)

Hi *,

i come from building swing applications - component oriented with MVC.
There we build a chain of components (Frames and panels which contains panels). The Controller in main panel communicates with database to get the data to represent. To get reusability the parent of a component sets data to child and so on. So we push the models downstairs.

Is there a wicket way in achieving this? Afaik setModel only sets the model of its component. I'm looking for an easy way to refresh the models of the childs. Because wicket is making many things different (no critism - it's ok for me) - maybe you do this different to.

Thanks for helping me out
Per
-- 
Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kanns mit allen: http://www.gmx.net/de/go/multimessenger

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


Re: HOWTO - update models in chain (push or pull)

Posted by MattClark <ma...@clarknet.cc>.
I'm attempting to accomplish the same thing, and am actually maintaining the
model in the user's session.  When I need to have two panels on the page
which show a different piece of the same model, I actually pass in the same
model.  To handle Ajax updating of both panels, I call a method on the model
and pass in the component that initiated the change, and then walk the page
hierarchy looking for anyone who cares that the model changed so they can
add themselves to the AjaxRequestTarget.

See:
http://www.nabble.com/Updating-distant-%28unrelated%29-components-via-Ajax-when-a-shared-model-changes-tf4610276.html

The resulting programming model feels a lot like Swing, in that I can set
something in a high level at the model and have all related components
repaint, but it's still using the pull model of Wicket.  The code I posted
in the referenced thread basically accomplishes what is handled for you in
Swing, which is having the framework repaint the widgets for you without
having to explicitly look them up and say 'repaint yourself'.  Does anyone
have any thoughts on whether this will work long-term in Wicket?

Thanks,
Matt


Newgro wrote:
> 
> Maybe im a bit swing-oriented. There we don't create always new page and
> panel 
> instances. Instead we reuse the created view instances and exchange simply 
> the model / models. So if (as an example) i change the order (while 
> navigating thru all orders) the related adress will change to.
> So i could always create the whole page for the new order again or
> exchange 
> the model.
> 
> Thanks
> Per
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/HOWTO---update-models-in-chain-%28push-or-pull%29-tf4612233.html#a13191539
Sent from the Wicket - User 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: HOWTO - update models in chain (push or pull)

Posted by Per Newgro <pe...@gmx.ch>.
Maybe im a bit swing-oriented. There we don't create always new page and panel 
instances. Instead we reuse the created view instances and exchange simply 
the model / models. So if (as an example) i change the order (while 
navigating thru all orders) the related adress will change to.
So i could always create the whole page for the new order again or exchange 
the model.

Thanks
Per

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


Re: HOWTO - update models in chain (push or pull)

Posted by Igor Vaynberg <ig...@gmail.com>.
why would the address object ever change? you are just pushing and pulling
its properties not the object itself...

-igor


On 10/12/07, Per Newgro <pe...@gmx.ch> wrote:
>
> Ahh. Ok. I can set the personpnel its model by calling
> orderpage extends webpage {
>   public orderpage(IModel order) {
>     add(new personpanel("shipto", new propertymodel(order,"shipto"));
>   }
> }
> invoicepage extends webpage {
>   public invoicepage(IModel invoice) {
>     add(new personpanel("billto", new propertymodel(invoice,"billto"));
>   }
> }
>
> So reusing of person component is save.
>
> Lets assume the order contains a person which contains an adress.
> I can access it with "order.getShipTo().getAdress()".
> I add the model for adesspanel the same way as for personpanel above.
>
> My question is if the order will be exchanged, will the subsequent
> adress-model be refreshed to? Isn't this model referencing the old adress
> in
> old order-model?
>
> Thanks having so much patience for me
> Per
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: HOWTO - update models in chain (push or pull)

Posted by Per Newgro <pe...@gmx.ch>.
Ahh. Ok. I can set the personpnel its model by calling
orderpage extends webpage {
  public orderpage(IModel order) {
    add(new personpanel("shipto", new propertymodel(order,"shipto"));
  }
}
invoicepage extends webpage {
  public invoicepage(IModel invoice) {
    add(new personpanel("billto", new propertymodel(invoice,"billto"));
  }
}

So reusing of person component is save.

Lets assume the order contains a person which contains an adress.
I can access it with "order.getShipTo().getAdress()". 
I add the model for adesspanel the same way as for personpanel above. 

My question is if the order will be exchanged, will the subsequent 
adress-model be refreshed to? Isn't this model referencing the old adress in 
old order-model?

Thanks having so much patience for me
Per

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


Re: HOWTO - update models in chain (push or pull)

Posted by Per Newgro <pe...@gmx.ch>.
Sorry for my stupidity. I think i got it now. I have to provide an "accessor 
chain" based on the page model assigned. So i don't have to get the objects 
in the model. Instead i have to specify the "path to object'.

Code below is working now.
 public HomePage(final PageParameters parameters) {
   OrderModel m = OrderModelFactory.create();
   IModel model = new CompoundPropertyModel(m);
   setModel(model);

   Panel pnlOrder = new OrderPanel("pnlOrder", 
                               new PropertyModel(model, "order"));
   pnlOrder.setOutputMarkupId(true);
   add(pnlOrder);

   Panel pnlShipTo = new PersonPanel("pnlShipTo", 
                                 new PropertyModel(model, "shipTo"));
   pnlShipTo.setOutputMarkupId(true);
   add(pnlShipTo);

   add(new AjaxLink("lnkNext") {
     public void onClick(AjaxRequestTarget target) {
       OrderModel m2 = OrderModelFactory2.create();
       HomePage.this.setModelObject(m2);
       target.addComponent(HomePage.this.get("pnlOrder"));
       target.addComponent(HomePage.this.get("pnlShipTo"));
     }
   });
 }

 public class OrderPanel extends Panel {

   /**
    * Constructor of OrderPanel.
    */
   public OrderPanel(String id, IModel m) {
     super(id);
     add(new Label("orderId", new PropertyModel(m, "orderId")));
     add(new Label("orderDate", new PropertyModel(m, "orderDate")));
   }
 }


 public class OrderModel implements Serializable {

   private Order order = null;

   public Order getOrder() {
     return order;
   }

   public void setOrder(Order pOrder) {
     order = pOrder;
   }
 }

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


Re: HOWTO - update models in chain (push or pull)

Posted by Per Newgro <pe...@gmx.ch>.
I tried the code below. Personpanel / model looks similar to orderpanel / 
model. OrderFactory2 creates another order with other data (quick and dirty). 
But the display of order is not changing. Why is this not working, if i share 
the model?

public HomePage(final PageParameters parameters) {
  OrderModel m = OrderModelFactory.create();
  IModel model = new CompoundPropertyModel(m);
  setModel(model);
  Panel pnlOrder = new OrderPanel("pnlOrder", m.getOrder());
  pnlOrder.setOutputMarkupId(true);
  add(pnlOrder);
  Panel pnlShipTo = new PersonPanel("pnlShipTo", m.getShipTo());
  pnlShipTo.setOutputMarkupId(true);
  add(pnlShipTo);
  add(new AjaxLink("lnkNext") {
    public void onClick(AjaxRequestTarget target) {
      OrderModel m2 = OrderModelFactory2.create();
      HomePage.this.setModelObject(m2);
      target.addComponent(HomePage.this.get("pnlOrder"));
      target.addComponent(HomePage.this.get("pnlShipTo"));
    }
  });
}

public class OrderPanel extends Panel {

  /**
   * Constructor of OrderPanel.
   */
  public OrderPanel(String id, Order m) {
    super(id);
    add(new Label("orderId", new PropertyModel(m, "orderId")));
    add(new Label("orderDate", new PropertyModel(m, "orderDate")));
  }
}


public class OrderModel implements Serializable {

  private Order order = null;

  public Order getOrder() {
    return order;
  }

  public void setOrder(Order pOrder) {
    order = pOrder;
  }
}

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


Re: HOWTO - update models in chain (push or pull)

Posted by Igor Vaynberg <ig...@gmail.com>.
they do push the model, but also notice how the two pushed models are
connected together by the same model object

-igor


On 10/12/07, Per Newgro <pe...@gmx.ch> wrote:
>
> > i dont really understand what you are talking about.
> >
> > you have a panel that can display a person right? and in an order you
> have
> > two instances of person: the order and the invoice, and you want to
> reuse
> > your panel?
> No. On one page i edit the order and on another page i edit the invoice.
> Both are unrelated to each other and will be "created" by pressing a menu
> link. But both contain an related person (order - shipTo and invoice
> billTo).
> And this person will be presented in the personpanel.
>
> > orderpage extends webpage {
> >   public orderpage(IModel order) {
> >         add(new personpanel("shipto", new propertymodel(order,
> "shipto"));
> >         add(new personpanel("billto", new propertymodel(order,
> "bilto"));
> >    }
> But thats not the way i read the other post until now in this thread.
> There
> was always the speech of setting the model to the page and let the panels
> get
> their data from the parent (e.g. page) model (pull by sub-component). But
> this example says - push the model into the sub-components.
>
> Thanks
> Per
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: HOWTO - update models in chain (push or pull)

Posted by Per Newgro <pe...@gmx.ch>.
> i dont really understand what you are talking about.
>
> you have a panel that can display a person right? and in an order you have
> two instances of person: the order and the invoice, and you want to reuse
> your panel?
No. On one page i edit the order and on another page i edit the invoice.
Both are unrelated to each other and will be "created" by pressing a menu 
link. But both contain an related person (order - shipTo and invoice billTo). 
And this person will be presented in the personpanel.

> orderpage extends webpage {
>   public orderpage(IModel order) {
>         add(new personpanel("shipto", new propertymodel(order, "shipto"));
>         add(new personpanel("billto", new propertymodel(order, "bilto"));
>    }
But thats not the way i read the other post until now in this thread. There 
was always the speech of setting the model to the page and let the panels get 
their data from the parent (e.g. page) model (pull by sub-component). But 
this example says - push the model into the sub-components.

Thanks
Per

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


Re: HOWTO - update models in chain (push or pull)

Posted by Igor Vaynberg <ig...@gmail.com>.
i dont really understand what you are talking about.

you have a panel that can display a person right? and in an order you have
two instances of person: the order and the invoice, and you want to reuse
your panel?

thats easy, lets say your order looks like this:
class order { person shipto; person billto; }

orderpage extends webpage {
  public orderpage(IModel order) {
        add(new personpanel("shipto", new propertymodel(order, "shipto"));
        add(new personpanel("billto", new propertymodel(order, "bilto"));
   }
}

-igor

On 10/12/07, Per Newgro <pe...@gmx.ch> wrote:
>
> Hello Igor Vaynberg:
> > see sourcecode to this:
> >
> > http://wicketstuff.org/wicket13/echo/
>
> Ok i see in the code that one model can be shared between two components.
> But
> my question was a bit more related to design.
>
> Lets say i build an order and an invoice page. If i read the posts in the
> threads and the wicket book - only the both pages get their models.
> Now both models contain the related person (order page model the OrderGuy
> and
> invoice page model the InvoiceGuy) which indeed will be designed equal in
> person-component (panel based person data presentation).
>
> If i have only one model in the page - how to reuse my person-component?
> Don't
> i have to make assumtions to the parent model design in my "lower"
> component
> because of the path to the related data in the model?
>
> Thanks alot
> Per
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: HOWTO - update models in chain (push or pull)

Posted by Per Newgro <pe...@gmx.ch>.
Hello Igor Vaynberg:
> see sourcecode to this:
>
> http://wicketstuff.org/wicket13/echo/

Ok i see in the code that one model can be shared between two components. But 
my question was a bit more related to design.

Lets say i build an order and an invoice page. If i read the posts in the 
threads and the wicket book - only the both pages get their models. 
Now both models contain the related person (order page model the OrderGuy and 
invoice page model the InvoiceGuy) which indeed will be designed equal in 
person-component (panel based person data presentation).

If i have only one model in the page - how to reuse my person-component? Don't 
i have to make assumtions to the parent model design in my "lower" component 
because of the path to the related data in the model?

Thanks alot
Per

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


Re: HOWTO - update models in chain (push or pull)

Posted by Igor Vaynberg <ig...@gmail.com>.
see sourcecode to this:

http://wicketstuff.org/wicket13/echo/

-igor


On 10/12/07, Per Newgro <pe...@gmx.ch> wrote:
>
> Hi Gwyn,
>
> > The "Wicket way" would be to decouple the data from the display by
> > not having each using component having to have it's own model, but by
> > having a 'parent' model used by both - by default, if the component
> > itself doesn't have a model, Wicket will look for a model in the
> > parent, so just set one on the page?
> And how is the component getting the appropriate property from the hughe
> model?
>
> I would like to stay a bit more on the component building way. If i have
> an adress panel on an invoice page and (the same) on an order page, do i
> have to assume that invoice and order page have the same model to? If they
> have their own model how can the address panel get the data?
>
> Cheers
> Per
> --
> GMX FreeMail: 1 GB Postfach, 5 E-Mail-Adressen, 10 Free SMS.
> Alle Infos und kostenlose Anmeldung: http://www.gmx.net/de/go/freemail
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: HOWTO - update models in chain (push or pull)

Posted by Per Newgro <pe...@gmx.ch>.
Hi Gwyn,

> The "Wicket way" would be to decouple the data from the display by
> not having each using component having to have it's own model, but by
> having a 'parent' model used by both - by default, if the component
> itself doesn't have a model, Wicket will look for a model in the
> parent, so just set one on the page?
And how is the component getting the appropriate property from the hughe model?

I would like to stay a bit more on the component building way. If i have an adress panel on an invoice page and (the same) on an order page, do i have to assume that invoice and order page have the same model to? If they have their own model how can the address panel get the data?

Cheers
Per
-- 
GMX FreeMail: 1 GB Postfach, 5 E-Mail-Adressen, 10 Free SMS.
Alle Infos und kostenlose Anmeldung: http://www.gmx.net/de/go/freemail

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


Re: HOWTO - update models in chain (push or pull)

Posted by Gwyn Evans <gw...@gmail.com>.
On Friday, October 12, 2007, 12:09:09 PM, Per <pe...@gmx.ch> wrote:

>> You have both panels using the same data, i.e. the same model or
>> parent model?  
> Every component has its own model. The model changes of Panel1 have
> to be reflected in model2 and Panel2 itself.
> I have a category which i can select in a tree. The category
> contains a field "url" which points to an image. This image will be
> displayed on panel2. If i click on the category howto refresh the url on panel2?

>> As for the UI itself, then when the page is re-displayed, both panels
>> will update with the new values automatically. If you're using AJAX to
>> do the updating, you'd have to explicitly add both panels to the redraw
>> 'target' to have them refresh, but on a normal request, the whole page
>> will get rewritten anyway.
> Thats the problem. With all my trys i refreshed the models, but the
> new data were not displayed. Because of this i would like to get the wicket way.

The "Wicket way" would be to decouple the data from the display by
not having each using component having to have it's own model, but by
having a 'parent' model used by both - by default, if the component
itself doesn't have a model, Wicket will look for a model in the
parent, so just set one on the page?

/Gwyn


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


Re: HOWTO - update models in chain (push or pull)

Posted by Swaroop Belur <sw...@gmail.com>.
>> I have a category which i can select in a tree. The category contains a
field "url" which points to an image. This image will be displayed on
panel2. If i click on the category howto refresh the url on panel2?

tried Something like this:

DefaultMutableTreeNode selectednode = ....
Object urobject = selectednode.getUserObject()

Assume u have category id stored in this object,
Then just add the other panel to ajaxrequesttarget.
While building that panel, you can pass this id to fetch
url

-swaroop


On 10/12/07, Per Newgro <pe...@gmx.ch> wrote:
>
> > You have both panels using the same data, i.e. the same model or
> > parent model?
> Every component has its own model. The model changes of Panel1 have to be
> reflected in model2 and Panel2 itself.
> I have a category which i can select in a tree. The category contains a
> field "url" which points to an image. This image will be displayed on
> panel2. If i click on the category howto refresh the url on panel2?
>
> > As for the UI itself, then when the page is re-displayed, both panels
> > will update with the new values automatically. If you're using AJAX to
> > do the updating, you'd have to explicitly add both panels to the redraw
> > 'target' to have them refresh, but on a normal request, the whole page
> > will get rewritten anyway.
> Thats the problem. With all my trys i refreshed the models, but the new
> data were not displayed. Because of this i would like to get the wicket way.
>
> Tanks
> Per
> --
> Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten
> Browser-Versionen downloaden: http://www.gmx.net/de/go/browser
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: HOWTO - update models in chain (push or pull)

Posted by Per Newgro <pe...@gmx.ch>.
> You have both panels using the same data, i.e. the same model or
> parent model?  
Every component has its own model. The model changes of Panel1 have to be reflected in model2 and Panel2 itself.
I have a category which i can select in a tree. The category contains a field "url" which points to an image. This image will be displayed on panel2. If i click on the category howto refresh the url on panel2?

> As for the UI itself, then when the page is re-displayed, both panels
> will update with the new values automatically. If you're using AJAX to
> do the updating, you'd have to explicitly add both panels to the redraw
> 'target' to have them refresh, but on a normal request, the whole page
> will get rewritten anyway.
Thats the problem. With all my trys i refreshed the models, but the new data were not displayed. Because of this i would like to get the wicket way.

Tanks
Per
-- 
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten 
Browser-Versionen downloaden: http://www.gmx.net/de/go/browser

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


Re: HOWTO - update models in chain (push or pull)

Posted by Gwyn Evans <gw...@gmail.com>.
On Friday, October 12, 2007, 11:17:19 AM, Per <pe...@gmx.ch> wrote:

> Hi Matej,

> Thanks for your answer. But how can i know in a child model beside
> the changed one that i have to pull my data again?
> E.g. if i have page with two panels. Panel1 executes a link and
> changes its data. The data change in Panel1 has to reflected in
> Panel2 to. My intension was to get a plan how i can go the wicket
> way to update the model in Panel2.

You have both panels using the same data, i.e. the same model or
parent model?  Take a look at CompoundPropertyModel.bind() (used
to require BoundCompoundPropertyModel, so search for that in the
Wiki's "New User Guide" page.

As for the UI itself, then when the page is re-displayed, both panels
will update with the new values automatically. If you're using AJAX to
do the updating, you'd have to explicitly add both panels to the redraw
'target' to have them refresh, but on a normal request, the whole page
will get rewritten anyway.


/Gwyn


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


Re: HOWTO - update models in chain (push or pull)

Posted by Per Newgro <pe...@gmx.ch>.
Hi Matej,

Thanks for your answer. But how can i know in a child model beside the changed one that i have to pull my data again?
E.g. if i have page with two panels. Panel1 executes a link and changes its data. The data change in Panel1 has to reflected in Panel2 to. My intension was to get a plan how i can go the wicket way to update the model in Panel2.

Thanks
Per
-- 
GMX FreeMail: 1 GB Postfach, 5 E-Mail-Adressen, 10 Free SMS.
Alle Infos und kostenlose Anmeldung: http://www.gmx.net/de/go/freemail

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


Re: HOWTO - update models in chain (push or pull)

Posted by Matej Knopp <ma...@gmail.com>.
Well, most of the time in wicket you don't update models. You pull
data from models, and that's also how you need to implement them. The
exception is of course when you process a form, in that case you push
data into models, which in turn update your beans, etc. But most of
the time (when rendering page/components) everything is pull.

-Matej

On 10/12/07, Per Newgro <pe...@gmx.ch> wrote:
> Hi *,
>
> i come from building swing applications - component oriented with MVC.
> There we build a chain of components (Frames and panels which contains panels). The Controller in main panel communicates with database to get the data to represent. To get reusability the parent of a component sets data to child and so on. So we push the models downstairs.
>
> Is there a wicket way in achieving this? Afaik setModel only sets the model of its component. I'm looking for an easy way to refresh the models of the childs. Because wicket is making many things different (no critism - it's ok for me) - maybe you do this different to.
>
> Thanks for helping me out
> Per
> --
> Psssst! Schon vom neuen GMX MultiMessenger gehört?
> Der kanns mit allen: http://www.gmx.net/de/go/multimessenger
>
> ---------------------------------------------------------------------
> 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