Custom Expression - AFS

AFS Integration Guide

Product
AFS
AFS_Version
7.7
Category
Reference Guide

Some cases are not handled by the built-in expressions. For instance advanced formatting or work with properties list.

In such cases, it is possible to implement data retrieval and formatting in JavaScript.

For that, declare a function that returns a string (which is the result of the retrieval and formatting) and that takes two parameters:

  • reply: the AFS reply object in JSON format
  • xmlClientData: the XML client-data extracted from the AFS reply (can also be found in reply)

Use the following expression syntax to call the function:

function:functionName

It will execute the function with the right parameters and display the function return.

A basic knowledge of JavaScript language programming is necessary to implement such functions.

To parse the XML stream, it is recommend to use a Javascript library such as jQuery.

Parsing XML in a cross browser way is something really difficult. See How To Parse XML Documents In Javascript? for more information.

Find hereafter two examples using jQuery (jquery.js must be included inside the page):

<!-- Somewhere in the page -->
<script type="text/javascript">
// Example function that works only with AFS standard reply properties
function extractUri(reply, xmlClientData) {
// Returns the uri property of the reply
return reply.uri;
}

// Example function that parses XML client data using JQuery
// Refer to this documentation to understand how to parse XML using JQuery:
// http://api.jquery.com/jQuery.parseXML/
// Let's assume XML Stream looks like that:
// <clientData>
// <author>Author name</author>
// <url>http://test.com/page.html</url>
// </clientData>
function extractUrlFromClientData(reply, xmlClientData) {
// Parse document using JQuery
var $clientDataDocument = $($.parseXML(xmlClientData));
// Selects the url node
var $urlNode = $clientDataDocument.find('url');
// Returns its string value
return $urlNode.text();
}
</script>

Functions are called thanks to function:extractUri for the first one and function:extractUrlFromClientData for the second one.