Javascript Callbacks
From Daxipedia
Contents |
[edit] About
Javascript callbacks allow developers to append their own javascript code to the DAX framework.
Blank callback functions are located in dev/callbacks.js file.
[edit] Callbacks
[edit] On Editor Pull
Executed when editor get the record from the 4D.
Parameters passed:
- selectionName - name of the current selection. Use this to filter which selections are processed.
- xmlRecord - record in raw XML format. XML structure is located at Query_Calls under Get Record command.
Following sample code will take raw XML record sent from 4D and change every social security number into 'XXX-XX-XXXX' string.
function callback_onEditorPull (selectionName, xmlRecord)
{
// custom code starts here
// get reference to field elements
var result = xmlRecord.responseXML.getElementsByTagName("queryResult").item(0);
var row = result.getElementsByTagName('row').item(0);
var fields = row.getElementsByTagName('field');
// loop through fields
for (l = 0; l < fields.length; l++) {
// check if field contains social security number, and if it does, mask it
if (getField(selectionName, fields[l].getAttribute('id')).fieldname == 'Person_ID')
fields[l].textContent = 'XXX-XX-XXXX';
}
// custom code ends here
// return the xml reply back to the DAX
return xmlRecord;
}
[edit] On Query
Executed when view request data from the 4D.
Parameters passed:
- selectionName - name of the current selection. Use this to filter which selections are processed.
- xmlRecord - record in raw XML format. XML structure is located at Query_Calls under Get Record command.
Following sample code will convert all names from the 'First_Name' field and replace them with an initial.
function callback_onViewQuery (selectionName, xmlRecord, viewType)
{
// custom code starts here
// get reference to field elements
var result = xmlRecord.responseXML.getElementsByTagName("queryResult").item(0);
var rows = result.getElementsByTagName('row');
// loop through rows
for (k = 0; k < rows.length; k++) {
// loop through fields
var fields = rows[k].getElementsByTagName('field');
for (l = 0; l < fields.length; l++) {
// check if this is 'First_Name' field and if it is replace name with initial
if (getField(selectionName, fields[l].getAttribute('id')).fieldname == 'First_Name')
fields[l].textContent = fields[l].substr(0,1);
}
}
// custom code ends here
// return the xml reply back to the DAX
return xmlRecord;
}
[edit] After Successful Record Save
Executed when record is successfully saved in editor.
Parameters passed:
- selectionName - name of the current selection. Use this to filter which selections are processed.
- recordId - record id returned from 4D in [x][y] format.
Following sample code will display an alert with selection name and record id.
function callback_afterRecordSaveSuccess (selectionName, recordId)
{
// custom code starts here
alert ('Record ' + recordId + ' from ' + selectionName + ' saved successfully.');
// custom code ends here
}
[edit] After Successful Login
Executed when user successfully logs in.
Parameters passed:
- username - name of the logged in user.
Following sample code will display an welcome message.
function callback_onLoginSuccess (username)
{
// custom code starts here
alert ('Welcome, ' + username + '.');
// custom code ends here
}
[edit] Before Record Save
Executed before saved record is sent to 4D, allowing developer to modify the data.
Parameters passed:
- selectionName - name of the current selection. Use this to filter which selections are processed.
- recordId - saved record id.
- recordData - data object with following arrays attached:
- .fieldId - array that holds id of the field. Used internally by DAX.
- .fieldName - name of the corresponding field.
- .value - value of the corresponding field.
Sample code (currently blank).
function callback_beforeRecordSave(selectionName, recordId, recordData)
{
// custom code starts here
// custom code ends here
//return data array back to the DAX
return recordData;
}
[edit] Error Trap
Traps the error returned from the 4D, and returns error hint and full error message.
function fourdaf_dev_errorTrap (hint, message){
}
