Skip to content

Instantly share code, notes, and snippets.

@jyeary
Last active November 9, 2018 20:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jyeary/8510341 to your computer and use it in GitHub Desktop.
Save jyeary/8510341 to your computer and use it in GitHub Desktop.
JSF AJAX client side handling code.
/*
* Copyright 2012-2014 John Yeary <jyeary@bluelotussoftware.com>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
jsf.ajax.addOnError(function(data) {
// This shows how to get the information via XPath, but it is not required. The error name can be found using data.errorName
var errorName = data.responseXML.evaluate('//error/error-name', data.responseXML, null, XPathResult.STRING_TYPE, null);
var message = 'AJAX Exception';
message += '\nSource: ' + data.source.id;
message += '\nValue:' + data.source.value;
message += '\nError: ' + errorName.stringValue;
message += '\nMessage: ' + data.errorMessage;
alert(message);
//TODO Take Additional actions.
});
jsf.ajax.addOnEvent(function(data) {
alert(data.source.id + " " + data.type + " " + data.status);
});
function handleAjax(data) {
var status = data.status;
switch (status) {
case "begin":
// This is the start of the AJAX request.
//TODO Take action here.
break;
case "complete":
// This is invoked right after AJAX response is returned.
//TODO Take action here.
break;
case "success":
// This is invoked right after successful processing of AJAX response and update of HTML DOM.
alert("The AJAX responseCode was: " + data.responseCode);
//TODO Take Additional actions.
break;
}
}
// Setup the statusUpdate function to hear all events on the page
jsf.ajax.addOnEvent(handleAjax);
@jyeary
Copy link
Author

jyeary commented Nov 9, 2018

It can be added to a particular page using:

<h:outputScript library="js" target="head" name="jsf.ajax.handler.js"/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment