WebApp modules

WebApps use RequireJS to load and export modules, using the CommonJS module format. Modules are stored in sub folders of the /module folder. The sub folders are /server, /common and /client, which represent a context the module should be loaded in.

  • module/
    • server/
      • JavaScript modules that will be executed server side. SiteVision's Public API may be used here, but not objects bound to the browser context.
    • client/
      • JavaScript modules that are used client side.
    • common/
      • JavaScript modules that are used both client and server side.

In the following examples modules are defined by returning a value. Note that the exports and module arguments are excluded.

// /module/server/myModule.js
define(function(require) {
   'use strict';

   var
      portletContextUtil = require('PortletContextUtil'),
      propertyUtil       = require('PropertyUtil');
   
   // Define this module
   return {
      getPropertyFromCurrentPage: function(prop) {
         return propertyUtil.getString(portletContextUtil.getCurrentPage(), prop);
      },

      add: function(a, b) {
         return a + b;
      }
   };
});
// index.js
(function() {
   'use strict';

   var 
      router   = require('router'),
      myModule = require('/module/server/myModule');

   router.get('/', function(req, res) {
      var data = {
         displayName: myModule.getPropertyFromCurrentPage('displayName'),
         result: myModule.add(1, 2)
      };
      
      return res.render('/', data);
   });
}());