Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Table of Contents
maxLevel3
minLevel2

Anchor
anc-defining-and-referencing
anc-defining-and-referencing
Defining, referencing and using a JavaScript model

Anchor
base-recipe
base-recipe

...


Code Pro
your-light-module/
└── templates
    └── pages
        ├── rhino.ftl
        ├── rhino.js
        └── rhino.yaml

This recipe works for both page and component templates.

Using the modelPath and class property

...

With this approach you do not have to follow the naming convention

...

. This can be handy

...

to use the same model

...

among multiple templates.

...

Instead of the property modelClass, you must set the properties class and modelPath. The value for the former must be info.magnolia.module.jsmodels.rendering.JavascriptTemplateDefinition and for the latter the path to the JavaScript model.

Example:

Code Pro
languageyaml
titlerhino.yaml
templateScript: /your-light-module/templates/pages/rhino.ftl
renderType: freemarker

...

class: info.magnolia.module.jsmodels.rendering.

...

JavascriptTemplateDefinition
modelPath: /your-light-module/templates/common/baseModel.js

Nashorn, JavaScript and the Java API

When you write a JavaScript model, you obviously write a JavaScript code. However, you will encounter objects which come from the Java world. For instance this is true when you work with Magnolia's built-in rendering context objects or when using templating functions.

Tip

When using objects originating in Java within a JavaScript model, it is helpful to know their public methods, which you can also use within the JavaScript code. To get familiar with these objects, it helps to have a look at the Java API pages which list the public methods and the corresponding return types. On this page you will find many links to Java docs. Follow the links to get an understanding of these Java-origin objects.

...

JavaScript can also be used for processing forms created by Magnolia's Form Module (see also Form module creating a custom form processor). Below is the form definition and the JavaScript based form processor for a form in order to create a new contact in the contacts workspace. 

...

You can expose any other component in the same way as enabling the templating functions, see JavaScript Models module - Exposing components. A component is a Java class (or an interface for which a type mapping exists within the framework) having a default constructor that can be resolved and instantiated by the IoC framework.

(warning) This is a very powerful mechanism and should be used carefully. Fortunately, its power can be limited by restricting access to the Java API.

Restricting access to the Java API

...