Modules Filters
Aria Templates comes out of the box with an embedded filters mechanism. More information are available in the Core Filters documentation.
Combined with the module layer, you can achieve really powerful things, such as emulating a server side.
Using the sender property
The first thing you need to do inside a filter you would have designed to be used with a module, is to limit its execution to only this module. When you do an asynchronous request from within your module, you are using submitJsonRequest(), which is implicitely using the RequestMgr on top of IO.
Though, to test from which module your request is coming inside your filter, you can easily just check inside your onRequest() or onResponse() methods.
Aria Template dynamically publishes for you properties on the request (or response) argument.
For example, for requests sent by the RequestMgr, here are the properties available in the sender property:
-
classpath: contains "aria.modules.RequestMgr" (allows to check if the request was made through theRequestMgr). -
requestObject: contains information about the module which did the request (itsmoduleNameandactionName). -
requestData: contains the posted data as a json object. If the filter needs to change it, it can modify this property, but it should then callsetJsonPostDatato update the encoded string inrequest.postData. -
responseData: null by default, this property can be set by a filter, and in this case, the request manager will use this value as thedataretrieved to the calling module (bypassing the connection handler) -
responseErrorData: null by default, this property can be set by a filter, and in this case, the request manager will use this value as theerrorDataretrieved to the calling module (bypassing the connection handler)
The following filter looks for requests done by the my.app.Controller module through the request manager:
File /modules/filters/TargetedFilter.js
$classpath:'ariadoc.snippets.modules.filters.TargetedFilter',
$extends:'aria.core.IOFilter',
$constructor:function(args) {
this.$IOFilter.constructor.call(this, args);
},
$prototype:{
onRequest:function(request) {
var sender = request.sender;
if (sender && sender.classpath == "aria.modules.RequestMgr"
&& sender.requestObject.moduleName == 'ariadoc.snippets.modules.filters.MyController') {
// ... Do your filtering here
}
}
}
Working Offline
One of the most interesting feature provided by filters is related to working offline.
Many times, it can be helpful for UI developers to have the ability to start working without an available back-end. You could even think to work without an application server, simply by mocking the data responses sent by the server.
IO filters can be used to intercept requests, and redirect to static XML files rather than let the calls go to your application server. Thanks to the filters being true classes, these redirection can even be made dynamic, i.e. based on requests parameters, different data can be returned.
Consider the following example:
- The filter class
File /modules/filters/OfflineFilter.js
$classpath:'ariadoc.snippets.modules.filters.OfflineFilter',
$extends:'aria.core.IOFilter',
$constructor:function(args) {
this.$IOFilter.constructor.call(this, args);
},
$prototype:{
onRequest:function(request) {
var sender = request.sender;
if (!sender || sender.classpath != "aria.modules.RequestMgr") {
return;
}
var responseFile = '';
var requestObject = sender.requestObject;
if (requestObject.moduleName == "ariadoc.snippets.modules.filters.MyController" && requestObject.actionName == "doStuff") {
var requestData = sender.requestData;
if (requestData == null) {
responseFile = '/mocks/response-dostuff-nodata.xml';
} else {
var param = requestData.param;
if (param == "option_1" || param == "option_2") {
responseFile = '/mocks/response-dostuff-option.xml';
}
else {
// let's send back default response
responseFile = '/mocks/response-dostuff-default.xml';
}
}
}
if (responseFile) {
this.redirectToFile(request, this.$package.replace(/\./ig, "/") + responseFile);
}
}
}
- The moduleCtrl class
File /modules/filters/MyController.js
$classpath: 'ariadoc.snippets.modules.filters.MyController',
$extends: 'aria.templates.ModuleCtrl',
$constructor: function() {
this.$ModuleCtrl.constructor.call(this, arguments);
aria.core.IOFiltersMgr.addFilter('ariadoc.snippets.modules.filters.OfflineFilter');
},
$desctructor: function() {
aria.core.IOFiltersMgr.removeFilter('ariadoc.snippets.modules.filters.OfflineFilter');
},
$prototype: {
doSomeAsyncStuff: function(option, callback) {
var data2send;
if (option) {
data2send = {
param: option
};
}
this.submitJsonRequest('doStuff', data2send, {
fn: "__onDoSomeAsyncStuff",
scope: this,
args: {
callback: callback
}
});
},
__onDoSomeAsyncStuff: function(result, args) {
//Get here your mocked datas.
}
}
In the previous example, requests are intercepted and, based on their action and json parameters, redirected to static XML files instead. The helper redirectToFile() method is used to simplify the redirection.
Creating the XML responses
The default handler of Aria Templates expects data responses from the server in form of XML messages. Creating these static XML files to work offline can be done in 2 ways:
- Writing them by hand.
- Recording actual "online" responses by copy/pasting the content into a XML file.
To record XML files, an IO filter can be used too. Aria Templates comes with an existing IO filter that can be used at run-time.
The IO filter's class to be used is aria.utils.filters.DisplayXmlResponse. Check its JavaScript documentation for help on how to use it.
