Scripting It Up

We've already seen how the Request Context, Datasources and View Templates work together. Out of the box we can see they're quite powerful; but sometimes you need a little bit more juice. 

That's why Expression comes with Javascript baked into the server.

Remember those Null Adapters? Let's look at an example:  

{
    "type"          : "XprNullAdapter",
    "contextName"   : "RandomNumber",  
    "outputProcessors" : [ "ScriptsBundle/SampleScript" ]
} 

outputProcessors is a key available to all adapter types. It supplies an array of bundlePaths to scripts which process the data in a filter chain:

Datasource-> (OutputProcessors...) -> ViewTemplate  

Let's look at our theoretical ScriptsBundle/SampleScript:

function process(inputData) {  
    //inputData is null, our Datasource doesn't fetch anything
    return {
                "Value" : Math.random().toString
           };  
}

The downstream View Template can now use {{RandomNumber.Value}}. Of course, this is just a toy example. Once you're familiarized with the functionality provided by the XPR Standard Library, you'll see how this powerful serverside scripts can really be.  

Note: Processors and postHandler scripts rely on a function named process taking context as the first parameter. The argument name for the first parameter of process can be semantic, e.g. LoginData, or PostData; and processors can have multiple inline functions, but the function name process must exist in the top namespace of the script so the scripting engine can find the entry point.