JBoss Community Archive (Read Only)

GateIn Portal 3.4

CommonJS

Introduction

This article provides tutorial on how to make JavaScript code, written in CommonJS module format, work with RequireJS in GateIn.

CommonJS Examples

Below is an example of two CommonJS modules and JavaScript code in template of my_portlet portlet that uses the two modules.

math.js
exports.add = function(a,b)
{
  return a + b;
};
increment.js
var add = require('math').add;
exports.increment = function(val)
{
  return add(val,1);
};
Template of my_portlet portlet
<div>

<script type="text/javascript">
//Business code using math.js and increment.js
var increment = require('increment').increment;
alert(increment(100));//Expect to see 101 in alert message
</script>

</div>

Native script

GateIn automatically wraps JavaScript code bundled under an ON_LOAD resource in one module definition block, that makes sense as underlying JavaScript code does not have AMD format but turns to break things as the code is already in AMD format. Native script feature helps us in case there is is need to escape JavaScript resource from automatic wrapping.

foo.js
 define("foo",["d1", "d2"], function(d1, d2)
 {
  ....
 });
  <module>
     <name>foo</name>
     <native-script>path_to_foo.js</native-script>
  </module>

Make CommonJS works in GateIn

The steps to make JavaScript code in CommonJS module format work in GateIn are:

  1. Wrap each CommonJS module in one AMD module definition block

  2. Declare each converted CommonJS module as native-script resource

  3. Wrap business code using CommonJS modules in callbacks passed to one require block of RequireJS

Convert CommonJS module format into AMD module definition block

We wrap each CommonJS module with a block

define([GTNPORTAL:"require", "exports", "module"], function(require, exports, module)
{

});

require, exports, module are built-in AMD modules of RequireJS

math.js
define(["require", "exports", "module"],function(require,exports,module){
exports.add = function(a,b)
{
  return a + b;
};
});
increment.js
define(["require", "exports", "module"], function(require,exports,module){
var add = require('math').add;
exports.increment = function(val)
{
  return add(val,1);
};
});

Declare math and increment as native-script resource

  <module>
    <name>math</name>
    <native-script>path_to_math.js</native-script>
  </module>

  <module>
    <name>increment</name>
    <native-script>path_to_increment.js</native-script>
  </module>

Wrap business code using math/increment in callback of RequireJS

Template of my_portlet
<div>

<script type="text/javascript">
require(["require", "math", "increment"], function(require)
{
//Business code using math.js and increment.js
var increment = require('increment').increment;
alert(increment(100));//Expect to see 101 in alert message
});
</script>

</div>
JBoss.org Content Archive (Read Only), exported from JBoss Community Documentation Editor at 2020-03-10 12:45:40 UTC, last content change 2012-08-31 04:35:05 UTC.