Helpers
Contents |
On top of Javascript Oriented Object concepts, Aria Templates also provides you methods through helpers to achieve all the most common and repetitive tasks that you will have to face everyday.
Some of them are directly bundled within the Core layer of the framework. The other one are available in the aria.utils package, ready for you to use them as a dependency inside your classes
Here is a non exhaustive list of what is available:
- Array, Object, Type
- Date
- Math, Random and Number
- Json
- Dom
- and many more you will discover by yourself...
Let's focus in this article on the 2 most important one, or at least, the 2 one you will have to use daily
Ajax Helper
The Ajax Helper is one of the helpers which is directly embedded inside the Core Layer. It's available for you to use through the aria.core.IO singleton class.
You can easily declare and execute an XHR call in your class within just a few lines of code.
The usage of this helper is mainly described in the Working in an Asynchronous World article. If not already done, we strongly encouraged you to read it.
JSON Helper
Working with Aria Templates means to manipulate a lot of JSON objects, and various data. This utility helper is designed to help you manage these sometimes complex manipulations.
Using this helper, you basically can:
- Serialize / Deserialize Json data using the 2 methods
convertToJsonString()andload()
File /core/helpers/JsonManipulation.js
employees: {
'stinson' : { firstname: "Barney", lastname: "Stinson" },
'mosby': { firstname: "Ted", lastname: "Mosby" },
'scherbatsky': { firstname: "Robin", lastname: "Scherbatsky" },
'eriksen': { firstname: "Marshal", lastname: "Eriksen" },
'kumar': { firstname: "Ranjit", lastname: "Kumar" }
},
contractors: {
'kumar': { company: 'TaxiDriver' }
}
}
getEmployeesListAsString: function() {
return this.__serialize(this.data.employees);
},
__serialize: function(myObject) {
return aria.utils.Json.convertToJsonString(myObject);
- Performs some tests over your objects, like checking whether this object contains this key with
contains(), or testing if a particular node is a metadata node withisMetadata(). You can also create a fresh new clone of an existing object usingcopy()
File /core/helpers/JsonManipulation.js
return aria.utils.Json.getValue(this.data.employees, employeeId);
},
updateEmployee: function(employeeId, firstname, lastname) {
aria.utils.Json.setValue(this.data.employees, employeeId, {
firstname: firstname,
lastname: lastname
});
},
injectExample: function() {
this.createEmployee("Aldrin", "Lily");
},
createEmployee: function(lastname, firstname) {
var employee_id = lastname.toLowerCase(),
new_employee = {
'lastname': lastname,
'firstname': firstname
},
employees = {};
employees[employee_id] = new_employee;
aria.utils.Json.inject(employees, this.data.employees, true);
- Apply the Observable pattern to any node of any of your Json object. What does it mean exactly ? It means that you can add listeners to your Json objects in order to be called back anytime a change is done to your data.
File /core/helpers/JsonManipulation.js
this.__addDefaultListeners();
// ....
// Then inside your $prototype
__addDefaultListeners: function() {
aria.utils.Json.addListener(this.data, 'employees', {
fn: "__employeesListChanged",
scope: this
});
},
/* As soon as the createEmployee() method is called, __employeesListChanged will be
automatically executed */
__employeesListChanged: function(change_set) {
this.$logInfo("The '%1' node has been updated. There are now %2 employees.",
[ change_set.dataName, (aria.utils.Object.keys(change_set.newValue)).length]);
Again, as you can see, the second argument of the aria.utils.Json.addListener() method is an Aria Templates callback object. Once inside your callback, you will be provided as argument an object that contains few properties:
- dataHolder: the object containing the property that has been changed
- dataName: the property name on the object
- oldValue: the previous value
- newValue: the new value
