Github user martin-g commented on a diff in the pull request:
https://github.com/apache/wicket/pull/151#discussion_r48414764
--- Diff:
wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxLazyLoadPanel.java
---
@@ -74,63 +106,74 @@ public AjaxLazyLoadPanel(final String id, final
IModel<?> model)
super(id, model);
setOutputMarkupId(true);
+ }
- add(new AbstractDefaultAjaxBehavior()
- {
- private static final long serialVersionUID = 1L;
-
- @Override
- protected void respond(final AjaxRequestTarget target)
- {
- if (state < 2)
- {
- Component component =
getLazyLoadComponent(LAZY_LOAD_COMPONENT_ID);
-
AjaxLazyLoadPanel.this.replace(component);
- setState((byte) 2);
-
AjaxLazyLoadPanel.this.onComponentLoaded(component, target);
- }
- target.add(AjaxLazyLoadPanel.this);
-
- }
-
- @Override
- protected void
updateAjaxAttributes(AjaxRequestAttributes attributes)
- {
- super.updateAjaxAttributes(attributes);
-
AjaxLazyLoadPanel.this.updateAjaxAttributes(attributes);
- }
-
- @Override
- public void renderHead(final Component component, final
IHeaderResponse response)
- {
- super.renderHead(component, response);
- if (state < 2)
- {
- CharSequence js =
getCallbackScript(component);
- handleCallbackScript(response, js,
component);
- }
- }
- });
+ /**
+ * Determines that the component we're waiting for is ready for
replacement, typically used in
+ * polling background threads for their result. Override this to
implement your own check.
+ *
+ * @return true when the LazyLoadPanel should replace its contents with
the actual component
+ */
+ protected boolean isReadyForReplacement()
+ {
+ return true;
}
- protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
+ /**
+ * Gets the spinner component shown when the lazy component is not
ready yet.
+ *
+ * @param markupId
+ * The components markupid.
+ * @return The component to show while the real component is being
created.
+ */
+ public Component getLoadingComponent(final String markupId)
{
+ IRequestHandler handler = new ResourceReferenceRequestHandler(
+ AbstractDefaultAjaxBehavior.INDICATOR);
+ return new Label(markupId,
+ "<img alt=\"Loading...\" src=\"" +
RequestCycle.get().urlFor(handler) + "\"/>")
+ .setEscapeModelStrings(false);
}
/**
- * Allows subclasses to change the callback script if needed.
+ * Factory method for creating the lazily loaded component that
replaces the spinner after
+ * {@link #isReadyForReplacement()} returns {@code true}. You may call
setRenderBodyOnly(true)
+ * on this component if you need the body only.
*
- * @param response
- * the current response that writes to the header
- * @param callbackScript
- * the JavaScript to write in the header
+ * @param markupId
+ * The components markupid.
+ * @return The component that must be lazy created.
+ */
+ public abstract Component getLazyLoadComponent(String markupId);
+
+ /**
+ * Called when the placeholder component is replaced with the lazy
loaded one.
+ *
* @param component
- * the component which produced the callback script
+ * The lazy loaded component
+ * @param target
+ * The Ajax request handler, can be null
*/
- protected void handleCallbackScript(final IHeaderResponse response,
- final CharSequence callbackScript, final Component component)
+ protected void onComponentLoaded(Component component, AjaxRequestTarget
target)
{
- response.render(OnDomReadyHeaderItem.forScript(callbackScript));
+ }
+
+ @Override
+ protected void onInitialize()
+ {
+ super.onInitialize();
+
+ // when the timer is not yet installed add it
+ AjaxLazyLoadTimer timer = getLazyLoadTimer();
+ if (timer == null)
+ {
+ timer = new AjaxLazyLoadTimer();
+ setLazyLoadTimer(timer);
+ getPage().add(timer);
--- End diff --
I think there is no need to keep two references to the timer in the page.
You can use `page.getBehaviors(AjaxLazyLoadTimer.class).get(0)` to get the
reference. No need of meta data.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---