Over a million developers have joined DZone.

Adding Module for Jaggery

· Integration Zone

Build APIs from SQL and NoSQL data sources in seconds. Read the Creating REST APIs white paper, brought to you in partnership with CA Technologies.

If you need any module or (jaggery code) to reuse in jaggery, we can add a module into jaggery easily.  I will be explain how to do it in this article.

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

image

[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"));
%>

image

The Integration Zone is brought to you in partnership with CA Technologies.  Create app backends instantly with REST APIs and reactive logic using CA Live API Creator.

Topics:

Published at DZone with permission of Madhuka Udantha , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}