You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by sv...@apache.org on 2018/02/06 15:08:30 UTC
wicket git commit: WICKET-6523 removed getJsTimeoutCall
Repository: wicket
Updated Branches:
refs/heads/WICKET-6523-ajax-timers ffcb8cfec -> 9c67d13e8
WICKET-6523 removed getJsTimeoutCall
added getTimerId instead; improved javadoc
Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/9c67d13e
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/9c67d13e
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/9c67d13e
Branch: refs/heads/WICKET-6523-ajax-timers
Commit: 9c67d13e8ba093af89f9bb01d2887d6c3d1bb6bc
Parents: ffcb8cf
Author: Sven Meier <sv...@apache.org>
Authored: Tue Feb 6 15:18:37 2018 +0100
Committer: Sven Meier <sv...@apache.org>
Committed: Tue Feb 6 15:18:37 2018 +0100
----------------------------------------------------------------------
.../wicket/ajax/AbstractAjaxTimerBehavior.java | 75 +++++++++++++-------
.../wicket/ajax/res/js/wicket-ajax-jquery.js | 7 +-
2 files changed, 53 insertions(+), 29 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/wicket/blob/9c67d13e/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractAjaxTimerBehavior.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractAjaxTimerBehavior.java b/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractAjaxTimerBehavior.java
index 94407ad..da947ee 100644
--- a/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractAjaxTimerBehavior.java
+++ b/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractAjaxTimerBehavior.java
@@ -98,30 +98,13 @@ public abstract class AbstractAjaxTimerBehavior extends AbstractDefaultAjaxBehav
}
/**
- * @param updateInterval
- * Duration between AJAX callbacks
- * @return JS script
- */
- protected final String getJsTimeoutCall(final Duration updateInterval)
- {
- CharSequence js = getCallbackScript();
-
- Component component = getComponent();
- // remember id for timer
- timerId = component.getMarkupId() + "." + component.getBehaviorId(this);
-
- return String.format("Wicket.Timer.set('%s', function(){%s}, %d);",
- timerId, js, updateInterval.getMilliseconds());
- }
-
- /**
*
* @see org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#respond(AjaxRequestTarget)
*/
@Override
protected final void respond(final AjaxRequestTarget target)
{
- // timerId is no longer valid after Ajax request
+ // timerId is no longer valid after timer has triggered
timerId = null;
if (shouldTrigger())
@@ -136,11 +119,11 @@ public abstract class AbstractAjaxTimerBehavior extends AbstractDefaultAjaxBehav
}
/**
- * Decides whether the timer behavior should render its JavaScript to re-trigger
- * it after the update interval.
+ * Decides whether the timer behavior should render its JavaScript to re-trigger it after the
+ * update interval.
*
- * @return {@code true} if the behavior is not stopped, it is enabled and still attached to
- * any component in the page or to the page itself
+ * @return {@code true} if the behavior is not stopped, it is enabled and still attached to any
+ * component in the page or to the page itself
*/
protected boolean shouldTrigger()
{
@@ -181,17 +164,55 @@ public abstract class AbstractAjaxTimerBehavior extends AbstractDefaultAjaxBehav
}
}
+ /**
+ * Create an identifier for the JavaScript timer.
+ * <p>
+ * Note: The identifier must not change as long as this behavior is attached to a component!
+ *
+ * @return creates an id based on {@link Component#getMarkupId()} and
+ * {@link Component#getBehaviorById(int)} by default
+ */
+ protected String getTimerId()
+ {
+ Component component = getComponent();
+
+ return component.getMarkupId() + "." + component.getBehaviorId(this);
+ }
+
+ /**
+ * Set the timeout on the given {@link IHeaderResponse}. Implementation note:
+ * <p>
+ * {@link #respond(AjaxRequestTarget)} might set the timer once and
+ * {@link #renderHead(Component, IHeaderResponse)} a second time successively, if the attached
+ * component is re-rendered on the same {@link AjaxRequestTarget}.
+ * <p>
+ * But rendering of the component might <em>not</em> actually happen on the same {@link AjaxRequestTarget},
+ * e.g. when a redirect to a full page-render is scheduled. Thus this method <em>always</em> sets the timeout
+ * and in the former case {@link AjaxRequestTarget} will take care of executing one of the
+ * two {@link OnLoadHeaderItem}s only.
+ *
+ * @param headerResponse
+ */
private void setTimeout(IHeaderResponse headerResponse)
{
- headerResponse.render(OnLoadHeaderItem.forScript(getJsTimeoutCall(updateInterval)));
+ CharSequence js = getCallbackScript();
+
+ // remember id to be able to clear it later
+ timerId = getTimerId();
+
+ headerResponse.render(
+ OnLoadHeaderItem.forScript(String.format("Wicket.Timer.set('%s', function(){%s}, %d);",
+ timerId, js, updateInterval.getMilliseconds())));
}
private void clearTimeout(IHeaderResponse headerResponse)
{
- if (timerId != null) {
- headerResponse.render(OnLoadHeaderItem.forScript("Wicket.Timer.clear('" + timerId + "');"));
-
- timerId = null;
+ if (timerId != null)
+ {
+ headerResponse
+ .render(OnLoadHeaderItem.forScript("Wicket.Timer.clear('" + timerId + "');"));
+
+ timerId = null;
}
}
http://git-wip-us.apache.org/repos/asf/wicket/blob/9c67d13e/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js b/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
index 794ccd6..f10ab04 100644
--- a/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
+++ b/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
@@ -2557,7 +2557,7 @@
},
/**
- * Un-schedules a timer by its id
+ * Clears a timer by its id
* @param {string} timerId - the identifier of the timer
*/
clear: function(timerId) {
@@ -2567,7 +2567,10 @@
}
},
- clearAll: function(timerId) {
+ /**
+ * Clear all remaining timers.
+ */
+ clearAll: function() {
var WTH = Wicket.TimerHandles;
if (WTH) {
for (var th in WTH) {