WebApp modules

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.

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

   var
      portletContextUtil = require('PortletContextUtil'),
      properties         = require('Properties');
   
   // Define this module
   return {
      getPropertyFromCurrentPage: function(prop) {
         return properties.get(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);
   });
}());	

Using modules for config [@since 5.0]

If index.js is present in config/ it is possible to utilize server modules for the configuration.

Below is such an example:

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

   const
      resourceLocatorUtil = require('ResourceLocatorUtil'),
      properties          = require('Properties');

   return {
      getAvailableFonts: function () {
         let
            fonts    = [],
            allFonts = resourceLocatorUtil.getFontRepository().getNodes();

         while (allFonts.hasNext()) {
            let font = allFonts.next();

            fonts.push(properties.get(font, 'displayName', 'selectorText'));
         }

         return fonts;
      }
   };
});
// config/index.js
const fontsProvider = require('/module/server/fontsProvider');
// ...
res.render({
	fonts: fontsProvider.getAvailableFonts()
});

// config/index.html
// ...
<select id="fontSelection" name="font" class="form-control">
	<% _.each(fonts, function(font) { %>
		<option value="<%- font.selectorText %>">
			<%- font.displayName %>
		</option>
	<% }) %>
</select>
// ...