سؤال

I know that I can subscribe to events using Wicket 6+ which is what I do in my application. Now I am trying to cancel an Ajax event on a particular occurrence of some condition like that:

Wicket.Event.subscribe('/ajax/call/before', function (jqEvent, 
    attributes, 
    jqXHR, 
    errorThrown, 
    textStatus) {
  if(someCondition) {
    // Abort event, but how?
  }
});

I am looking for a way to abort the event, but the normal jQuery event handlers

jqEvent.stopImmediatePropagation();
jqEvent.preventDefault();

or even

attributes.event.stopImmediatePropagation();
attributes.event.preventDefault();

but they do not seem to work. If the method returns a value, this does not seem to have an effect either. The easiest I found so far is simply throwing an exception but this solution is far from clean.

هل كانت مفيدة؟

المحلول

It is rather hacky but by following down the stack I found a solution to this. Wicket treats the component's callback handlers privileged. If you are registering an IAjaxCallListener, it will be allowed to return false which is read by Wicket and what stops the propagation. The "global" event listeners are for some reason denied this privilege which is why the return value must be smuggeled into an not-yet existing array of precondition handlers. It's messy but it works.

Wicket.Event.subscribe('/ajax/call/before', function (jqEvent, 
    attributes, 
    jqXHR, 
    errorThrown, 
    textStatus) {
      if (precondition) {
        attributes.pre = [stopWicket];
      }
    }
});

function stopWicket() {
    return false;
}

Anyways. who calls something a "precondition" but ignores the decision made on this precondition handler....

نصائح أخرى

A subscriber cannot stop Ajax processing.

Why not use a precondition for this? See AjaxRequestAttributes#getAjaxCallListeners() and IAjaxCallListener#getPrecondition().

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top