[
https://issues.apache.org/jira/browse/WICKET-6055?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15071014#comment-15071014
]
ASF GitHub Bot commented on WICKET-6055:
----------------------------------------
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.
> AjaxLazyLoadPanel should provide non-blocking lazy load
> -------------------------------------------------------
>
> Key: WICKET-6055
> URL: https://issues.apache.org/jira/browse/WICKET-6055
> Project: Wicket
> Issue Type: Improvement
> Components: wicket-extensions
> Affects Versions: 7.1.0
> Reporter: Martijn Dashorst
> Assignee: Martijn Dashorst
>
> When having multiple AjaxLazyLoadPanels on your page, they all block their
> Wicket request thread until the content is ready to load. This can be
> problematic when you try to wait for some background job to finish and want
> to poll for that job to be ready, and only then update the contents.
> The improvement would be to add a method that gives the developer the option
> to not update just yet (isReadyForReplacement) and when it returns true,
> start the replacement. By default this would return true, implementing the
> current behavior of the AjaxLazyLoadPanel.
> Furthermore to improve the responsiveness of the ALLP it should add a single
> timer to the page that can be used by multiple ALLPs to update themselves.
> The timer would poll each second and the ALLPs would use Wicket's event bus
> to update themselves. With some reference counting, the timer can remove
> itself from the page when all ALLPs have updated themselves.
> This enables refreshing the page as well when outside an AJAX context, or
> having a user be impatient and pressing F5.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)