Skip to content

3.7 Application Helpers

russa edited this page May 9, 2017 · 9 revisions

Application Helpers (www/helpers)

Helpers are something of a bridge between controllers and views. They can be used to generate dynamic view-content. In order to respect the MVC pattern, view-specifications should be preferred over helpers wherever possible. If this is not possible, helpers should be used with caution, since they can circumvent the strict separation between controllers and views.

Each controller can optionally have a helper definition. This helper is specified in the folder www/helpers. On startup, this folder is scanned and all helper definitions are automatically loaded.

The file containing the helper definition must have the controller's name as prefix followed by Helper.js, e.g. for controller Application, the helper would be specified in the file www/helpers/applicationHelper.js. In this case, the class for the helper would be ApplicationHelper:

attention
  var ApplicationHelper = function(){
    this.name = "ApplicationHelper";
  };
  ApplicationHelper.prototype.someFunction = function(ctrl, data){
	return "Test User";
  };
}

The functions specified in the helper can be invoked from views of the controller, and the String of the return value will be written into the location from where the helper function was called, for example the following statement in a view of the Application controller

attention

<p>Hello, @helper("someFunction")</p>

would mean that on rendering the view, that function someFunction in ApplicationHelper is invoked with the arguments of controller instance and the data argument that was used when issuing the rendering (see API for DialogManager.render(viewName, data); details for @helper expression). Then the result of the function call would be inserted into the view, which would result in this example to:

attention
<p>Hello, Test User</p>

For more details on *template expression*, see sections [Layout Template Expressions](https://github.com/mmig/mmir/wiki/3.8.1-Template-Expressions#layout-template-expressions) and [View Template Expressions](https://github.com/mmig/mmir/wiki/3.8.1-Template-Expressions#view-template-expressions) for template expressions that can be used within template files for views.

< previous: "Application Controllers" | next: "Application Views" >

Clone this wiki locally