Adding Module for Jaggery
We can reuse modules from jaggery applications in each particular jaggery server. I need to add some custom new JavaScript APIs that are mainly for your applications. Modules are he best option you have.
We can write a module in javascript or java (hostobject). In the module('<jaggery-home>\modules\') you have to point out the particular file with its location. Here I am explaining how to achieve this.
[1]Create a folder with your module name inside a '<jaggery-home>\modules\' (My module is for 'phone')
Create a module.xml inside this folder. Add your module names like you see below
<module name="phone" xmlns="http://wso2.org/projects/jaggery/module.xml"> <script> <name>phone</name> <path>scripts/phone.js</path> </script> </module>
[2] Add the js file in ‘scripts’ (Phone will have type and number, we can get phone information from the object – the phone I own)
phone.js
var phone = function (number) {
this.type = "Apple";
this.number = number;
this.getInfo = getPhoneInfo;
}
var getPhoneInfo = function () {
return this.number + ' is ' + this.type + ' phone';
}[3] Now start Jaggery server, jaggery will have a javascript API for Phone that we create. Let's try that now.
[4] Create jaggery Applications ‘testPhone’ contains ‘phone.jag’ Here is a jaggery file that I am going to use with the phone API that we created.
<%
function getPhone(phoneNumber){
var phone = require('phone');
var myPhone = new phone.phone(phoneNumber);
return myPhone.getInfo();
}
print(getPhone("94774066666"));
%>
[5] now go to http://localhost:9763/testPhone/phone.jag
[6] Now think you buy new Phone and your phone type is now changed to Nokia
<%
function getPhone(phoneNumber){
var phone = require('phone');
var myPhone = new phone.phone(phoneNumber);
//buy new phone
myPhone.type = "Nokia";
return myPhone.getInfo();
}
print(getPhone("94774066666"));
%>
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)





