Uploaded image for project: 'Magnolia CLI'
  1. Magnolia CLI
  2. NPMCLI-137

Create JavaScript model for page and component template optionally


    • Icon: New Feature New Feature
    • Resolution: Fixed
    • Icon: Neutral Neutral
    • 3.0.0
    • None
    • None
    • Kromeriz 125
    • 3


      With the introduction of the JavaScript Models module - models can be used in light-modules, the JS syntax makes them well suitable for frontend developers.

      The commands create-page and create-component should be able to create a JS model optionally.
      Proposed option: -m or -jsm.


      On the template definition add this line:

      modelClass: info.magnolia.module.jsmodels.rendering.JavascriptRenderingModel

      Create an additional file in the same directory as the template def. and script are created. File name must be <template-name>.js.
      Add dummy code to the model file such as this:

       * Define properties and methods which can be accessed on the template script.<br/>
       * You can use Magnolia rendering context objects content, def, ctx, state, i18n.<br/>
       * For further info see https://documentation.magnolia-cms.com/display/DOCS/JavaScript+Models+module.<br/>
      var Foobar = function () {
          this.getRandomNumber = function () {
              return Math.random();
      new Foobar();

      As discussed in the PR, we used prototype with javascript model. The solution above has changed, command for creating a template with js model is following:
      mgnl create-<page/component> newPageOrComponent -P with-js-model

        Acceptance criteria

              jsimak Jaroslav Simak
              cmeier Christoph Meier
              2 Vote for this issue
              3 Start watching this issue