You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by MattClark <ma...@clarknet.cc> on 2007/10/12 00:10:04 UTC

Updating distant (unrelated) components via Ajax when a shared model changes

All,

We're building an application which in some respects needs to behave like a
desktop client, where actions in one part of the UI affect components in
another portion of the UI.  Our specific case was that we had a left-nav
which contained some links, and a content area which contains an
AjaxTabbedPanel.  The links in the left-nav need to be rendered
visible/invisible based on the type of content in the currently selected
tab.

Since we didn't want to have a hard reference from the tab panel to the left
nav panel (because we wanted them to be relatively reusable components), we
decided to create a custom model that is shared between the two.  We then
used the attached class/interfaces to:

1) Enable the tab panel to notify the tab model that it has changed it via
Ajax
2) Enable the model in turn to notify the left-nav panel that its model has
changed
3) Allow the left-nav panel to re-render itself by adding it to the
AjaxRequestTarget

So, in our implementation we have a TabModel, which extends AjaxAwareModel. 
The LefNavPanel implements AjaxModelAware, and has an indirect reference to
the TabModel. When the currently selected tab is changed via Ajax in the
AjaxTabbedPanel, we call getTabModel().notifyModelChanged(this, target). 
The AjaxAwareModel code then looks for any component in the page which
extends AjaxModelAware, and notifies them that the tab model changed.

I'm posting this code because 1) I hope it will prove useful to someone
else, and 2) I'd appreciate any feedback, as I'd rather learn about major
design issues I overlooked sooner rather than later!  I see two improvements
we could make.  First, add generics to AjaxModelAware, so we can reduce the
number of components who are notified about the model changing by only
looking for components implementing the typed AjaxModelAware interface. 
Second, move the code in AjaxAwareModel into some sort of helper class so
that models can participate in this mechanism without having to extend that
class.

BTW, thanks to Frank Bille who actually started me down the path which led
to this.

http://www.nabble.com/file/p13165777/AjaxAwareModel.java AjaxAwareModel.java 
http://www.nabble.com/file/p13165777/AjaxModelAware.java AjaxModelAware.java 
http://www.nabble.com/file/p13165777/AjaxUpdateAware.java
AjaxUpdateAware.java 

Thanks in advance for any thoughts!
Matt Clark
-- 
View this message in context: http://www.nabble.com/Updating-distant-%28unrelated%29-components-via-Ajax-when-a-shared-model-changes-tf4610276.html#a13165777
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: Updating distant (unrelated) components via Ajax when a shared model changes

Posted by MattClark <ma...@clarknet.cc>.
FWIW, I had to make an update to AjaxAwareModel and AjaxModelAware. 
AjaxModelAware now returns a boolean, which signifies whether the
AjaxAwareModel should continue to traverse deeper in the hierarchy.  If the
parent component intends to re-render or replace its children (such as in
the case where it contains a refreshingview which will recreate all
children), and therefore does not want its children to add themselves to the
ajaxrequesttarget, then it should return false to stop deeper traversal.

New visitor in AjaxAwareModel:
page.visitChildren(new IVisitor(){
	
				public Object component(Component component) {
					if( component instanceof AjaxModelAware ){
						log.debug("...Notifying: " + component.getClassRelativePath() );
						if( ((AjaxModelAware)component).notifyModelChanged(target,
AjaxAwareModel.this) )
							return CONTINUE_TRAVERSAL;
					}
					return CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER;
				}
				
			});

New method signature in AjaxModelAware:
public boolean notifyModelChanged(AjaxRequestTarget target, AjaxAwareModel
model);
-- 
View this message in context: http://www.nabble.com/Updating-distant-%28unrelated%29-components-via-Ajax-when-a-shared-model-changes-tf4610276.html#a13192400
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