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().
Related
I'm working on a site that has a global Ajax error handler in the main page template, like this:
$(document).ajaxError(function (event, request, settings, thrownError) {
postError(thrownError);
})
This updates the page with information about an error from an Ajax request. I am working on a specific Ajax call where I need errors to be handled differently, it will just silently select a value without doing anything about the error. The function is in a handler for a DevExtreme SelectBox that must return a Promise:
return $.ajax({
// stuff
}).fail(function (request, status, error) {
// set value
});
This does what its supposed to in regards to setting the value, but the Ajax error handler still fires. I've tried setting the error to null in the .fail() function but it still gets posted. Is there a way to disable the ajaxError handler for just this one call?
Seems like you want to prevent bubbling the error up for a particular case. See the following post and I believe this is what you are looking for:
Javascript: How to stop multiple jQuery ajax error handlers?
Hope this helps!
Using Jquery I need to trigger a ajaxComplete event.
At the moment I'm using this code with no success
$.getJSON(assetUrl, function (data) {
...
$.trigger("ajaxComplete");
With Error:
TypeError: 'undefined' is not a function (evaluating '$.trigger("ajaxComplete")')
Any idea what I'm doing wrong? Thanks
The ajaxCompleted event is fired on the DOM, and you will need to call the trigger method on a jQuery wrapper element: $(document).trigger(...), for example.
There is not static function "trigger" on the jQuery object (that's what the error message is telling you), you might use $.event.trigger - though I fear that's internal.
However, you won't need to do it manually; getJSON does trigger the event itself. For aborting a running ajax request, see the abort method of XHR objects.
You can define a global jQuery ajaxComplete (and ajaxError) function that will run on document ready and after every completed ajax request. You can define the ajaxComplete function on the intial page load (or whenever really) like this:
$(function(){
$(document).ajaxComplete(function(){
// on complete code
}).ajaxError(function(){
// on error code
});
});
To call this event handler at any time, just execute the following:
$(document).triggerHandler('ajaxComplete');
If anybody else is looking at this, the correct way to manually trigger ajaxComplete is $(document).trigger('ajaxComplete', [xhr, settings]);
It's probably important to pass the xhr object to the ajaxComplete trigger event, as the event handler might need it.
However, you only need this, if you're not making your requests through jquery, since jquery handles this automatically for you.
I am quite new to nodeJS and Socket.IO and am facing a problem.
Is there any way to know the type of event inside a Socket.IO handler? I have this kind of code:
// Fire the appropriate callback when we receive a message
// that.received is an array of callback functions
for (var event in that.received) {
socket.on(event, function(message) {
// Of course, this won't work because of "event" scope
that.received[event](message, this);
});
}
So, what I want to know is the actual value of "event" that triggered my handler.
I tried to inspect available variables with Chrome developer tools, but I wasn't able to find anything.
I need to do this because I am writing some kind of wrapper class around Socket.IO to handle multiple sockets (a long storty about fallback servers). I want it to be generic enough to pass my handlers to it.
Any ideas?
OK, stupid question.
I just need to do this:
// Fire the appropriate callback when we receive a message
for (var event in that.received) {
socket.on(event, that.received[event]);
}
I was passing "this" to my callback function, which is just stupid. My callbacks looked like this:
function myCallback(message, socket) {
// Some code...
socket.emit('ack', message.id);
// Some code...
}
But all I have to do is this:
function myCallback(message) {
// Some code...
this.emit('ack', message.id);
// Some code...
}
So I don't have the scope problem anymore.
I've inherited a backbone js based app. I really like backbone and i'm just starting to get my head around it. From my understanding when model.save is called on a new entity it should post that to the server, the server should return the same json but with an id alloted and backbone should persist that id to the model so that further saves result in a PUT with the ID for update.
However, when I call model.save() and then try to get the model.id property, it's null.
Is this because I'm not doing it with a call back? So the property hasn't been set yet?
How would I set the success callback? calling model.save({success: function(){...}})doesn't work?
here is the actual call:
model.save(null, {
success: function () {
alert('success');
},
error: function () {
alert('error');
}
});
Something feels odd about this. Setting silent: true only makes it so none of the events get fired. Everything else should happen normally. In other words, don't assume that setting slient: true is the right answer here...
I suspect you are actually throwing an exception some place (probably with validation or something like that) and somehow, setting silent: true is causing everything to flow through.
I would strongly suggest that you remove this option and check your console or run with the debugger... I suspect you have a bug lurking around there some place.
Some suggestions: Take a look at the annotated source for the model.set function. It gets called before your success callback will get called. Inside of that function, there are several things that will happen if silent is false. These include validation, individual property change triggers, and a global change trigger. I would bet money that either the validation is failing or something that is listening to the changes is throwing an exception.
i needed to set the silent: true on the save:
model.save(null, {
silent: true,
success: function () {
alert('success');
},
error: function () {
alert('error');
}
});
I had the same issue, turned out to be because my custom parse was failing
I'm just playing around for the first time with jQuery's ajax functionality. I wanted to add a function that could handle any errors. So, in one of my client javascript blocks, I added the following line:
<script type="text/javascript">
....
$.ajax({ error: function () { alert('boo'); } })
....
</script>
I expected that this would bind the error handler, so that when an error occurs, it would fire the anonymous function included.
What happens instead though, is that it immediately fires the function on page load, as soon as it parses this line of code.
What am I doing wrong? What is the proper way to bind the ajax error handler?
I'm not sure if I understood your question correctly, let me know if I've misunderstood.
I assume that you are trying to create a generic ajax call error handler? If that's the case, you have got the wrong idea.
Are you are just trying to bind the event handler? In this case, you are executing it.
I would recommend you read and check out the examples on these jQuery API reference docs:
API/1.3/Events
Ajax/jQuery.ajax
Also check out the post link provided by F.Aquino and this SO post: JavaScript Exception Handling.
This is could be helpful too: Handling AJAX Errors With jQuery.
You want to change the global settings. Check jQuery documentation.
$.ajaxSetup({
error: function () { alert('boo'); }
});