-
-
Save wave-inguane/e5396f1b2aa39e5b6228fef32332e4d8 to your computer and use it in GitHub Desktop.
GlideAjax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Script Include | |
var GlobalClientUtil = Class.create(); | |
GlobalClientUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, { | |
isWigGoal : function() { | |
var result = this.newItem("result"); | |
result.setAttribute("message", "returning all data"); | |
var flag = ACLUtil.canReadWigGoals(); | |
this._addData("wig_goal", flag); | |
}, | |
_addData : function(name, value) { | |
var data = this.newItem("data"); | |
data.setAttribute("name", name); | |
data.setAttribute("value", value); | |
}, | |
type: 'GlobalClientUtil' | |
}); | |
//Client Script | |
function onLoad() { | |
if(g_form.isNewRecord()){ | |
var ga = new GlideAjax("GlobalClientUtil"); | |
ga.addParam("sysparm_name", "isWigGoal"); | |
ga.getXML(ajaxResponse); | |
} | |
function ajaxResponse(serverResponse) { | |
var result = serverResponse.responseXML.getElementsByTagName("result"); | |
var message = result[0].getAttribute("message"); | |
//Debug | |
// if(message) | |
// alert(message); | |
//Debug | |
//var output = ""; | |
var data = serverResponse.responseXML.getElementsByTagName("data"); | |
for(var i = 0; i < data.length; i++) { | |
var name = data[i].getAttribute("name"); | |
var value = data[i].getAttribute("value"); | |
if(name == 'wig_goal') | |
g_form.setValue('u_wig_goal',value); | |
//Debug | |
//output += name + " = " + value + "\n"; | |
} | |
//Debug | |
//alert(output); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//======================================================================================================== | |
// Client Callable | |
//======================================================================================================== | |
---*--Returning Multiple Values--*--- | |
//........................................................................................................ | |
//Script include: | |
//........................................................................................................ | |
/* | |
* MyFavoritesAjax script include Description - sample AJAX processor | |
returning multiple value pairs | |
*/ | |
var MyFavoritesAjax = Class.create(); | |
MyFavoritesAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, | |
{ | |
/* | |
* method available to client scripts call using: | |
* var gajax = new GlideAjax("MyFavoritesAjax"); | |
* gajax.addParam("sysparm_name", "getFavorites"); | |
*/ | |
getFavorites : function() { | |
// build new response xml element for result | |
var result = this.newItem("result"); | |
result.setAttribute("message", "returning all favorites"); | |
//add some favorite nodes with name and value attributes | |
this._addFavorite("color", "blue"); | |
this._addFavorite("beer", "lager"); | |
this._addFavorite("pet", "dog"); | |
// all items are returned to the client through the inherited methods of AbstractAjaxProcessor | |
}, | |
_addFavorite : function(name, value) { | |
var favs = this.newItem("favorite"); | |
favs.setAttribute("name", name); | |
favs.setAttribute("value", value); | |
}, | |
type : "MyFavoritesAjax" | |
}); | |
//......................................................................................................... | |
// Client script: | |
//......................................................................................................... | |
Client Script | |
// new GlideAjax object referencing name of AJAX script include | |
var ga = new GlideAjax("MyFavoritesAjax"); | |
// add name parameter to define which function we want to call | |
// method name in script include will be getFavorites | |
ga.addParam("sysparm_name", "getFavorites"); | |
// submit request to server, call ajaxResponse function with server response | |
ga.getXML(ajaxResponse); | |
function ajaxResponse(serverResponse) { | |
// get result element and attributes | |
var result = serverResponse.responseXML.getElementsByTagName("result"); | |
var message = result[0].getAttribute("message"); | |
//check for message attribute and alert user | |
if(message) | |
alert(message); | |
//build output to display on client for testing | |
var output = ""; | |
// get favorite elements | |
var favorites = serverResponse.responseXML.getElementsByTagName("favorite"); | |
for(var i = 0; i < favorites.length; i++) { | |
var name = favorites[i].getAttribute("name"); | |
var value = favorites[i].getAttribute("value"); | |
output += name + " = " + value + "\n"; | |
} | |
alert(output); | |
} | |
---*--Returning Single Values--*--- | |
//======================================================================================================== | |
// Client Callable | |
//======================================================================================================== | |
//........................................................................................................ | |
//Script include: | |
//........................................................................................................ | |
var GetEmailAddress = Class.create(); | |
// Extend the global.AbstractAjaxProcessor class | |
GetEmailAddress.prototype = Object.extendsObject(global.AbstractAjaxProcessor,{ | |
// Define the getEmail function. | |
// Create a GlideRecord for the User table. | |
// Use the sysparm_userID passed from the client side to retrieve a record from the User table. | |
// Return the email address for the requested record | |
getEmail: function() { | |
var userRecord = new GlideRecord("sys_user"); | |
userRecord.get(this.getParameter('sysparm_userID')); | |
return userRecord.email + ''; | |
}, | |
type: 'GetEmailAddress' | |
}); | |
//......................................................................................................... | |
// Client script: | |
//......................................................................................................... | |
function onChange(control, oldValue, newValue, isLoading, isTemplate) { | |
// Modified the if to return if the newValue == oldValue to avoid | |
// unecessary trips to the server | |
if (isLoading || newValue === '' || newValue == oldValue) { | |
return; | |
} | |
// Instantiate the GetEmailAddress Script Include | |
var getEmailAddr = new GlideAjax('GetEmailAddress'); | |
// Specify the getEmail method | |
getEmailAddr.addParam('sysparm_name','getEmail'); | |
// Pass the Requested for sys_id | |
getEmailAddr.addParam('sysparm_userID', g_form.getValue('u_requested_for')); | |
// Send the request to the server | |
getEmailAddr.getXML(populateEmailField); | |
// When the response is back from the server | |
function populateEmailField(response){ | |
// Extract the email address from the response, clear any value from the email field, | |
// set new value in the email field | |
var emailFromScriptInclude = response.responseXML.documentElement.getAttribute("answer"); | |
g_form.clearValue('u_requested_for_email'); | |
g_form.setValue('u_requested_for_email',emailFromScriptInclude); | |
} | |
} | |
//......................................................................................................... | |
// Synchronous Glide Ajax -- DO NOT USE | |
//......................................................................................................... | |
/* | |
If your use case demands that no further processing can occur until the GlideAjax response has been received, you | |
can use getXMLWait(). However, because this will slow down your code and lock the user session until the | |
response is received, it is generally recommended that you use getXML() with a callback function. | |
*/ | |
//The server-side script include code. | |
var HelloWorld = Class.create(); | |
HelloWorld.prototype = Object.extendsObject(AbstractAjaxProcessor, { | |
helloWorld: function() { | |
return "The Server Says Hello " + this.getParameter('sysparm_user_name') + "!"; | |
} | |
}); | |
//The client code. | |
var ga = new GlideAjax('HelloWorld'); | |
ga.addParam('sysparm_name','helloWorld'); | |
ga.addParam('sysparm_user_name',"Bob"); | |
ga.getXMLWait(); | |
alert(ga.getAnswer()); | |
//......................................................................................................... | |
// Asynchronous Glide Ajax | |
//......................................................................................................... | |
//Server side | |
var HelloWorld = Class.create(); | |
HelloWorld.prototype = Object.extendsObject(AbstractAjaxProcessor, { | |
helloWorld: function() { | |
return "Hello " + this.getParameter('sysparm_user_name') + "!"; | |
}, | |
_privateFunction: function() { // this function is not clientcallable | |
//Dpo some | |
} | |
}); | |
//========================================================================================================================== | |
// | |
//========================================================================================================================== | |
//Client side | |
var ga = new GlideAjax('HelloWorld'); | |
ga.addParam('sysparm_name','helloWorld'); | |
ga.addParam('sysparm_user_name',"Bob"); | |
ga.getXML(HelloWorldParse); | |
function HelloWorldParse(response) { | |
var answer = response.responseXML.documentElement.getAttribute("answer"); | |
alert(answer); | |
} | |
/* | |
======================================================================================================== | |
Server Callable | |
Usage: in Reference qualifier | |
======================================================================================================== | |
*/ | |
//Script include: | |
var ShowRegions = Class.create(); | |
ShowRegions.prototype = { | |
initialize: function() { | |
}, | |
// Usage - Reference qualifier | |
// javascript: new global.ShowRegions().regions(); | |
regions: function() { | |
var grDEP = new GlideRecord('cmn_department'); | |
grDEP.addEncodedQuery("nameSTARTSWITHR^parentISEMPTY^u_active=true"); | |
grDEP.query(); | |
var regionList; | |
while(grDEP.next()) { | |
regionList += ","+grDEP.sys_id; | |
} | |
return 'sys_idIN'+regionList; | |
}, | |
type: 'ShowRegions' | |
}; | |
//Server side: | |
// Usage - Reference qualifier | |
// javascript: new global.ShowRegions().regions(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment