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

Create JavaScript model for page and component template optionally

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Neutral
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.0.0
    • Labels:
      None
    • Epic Link:
    • Sprint:
      Kromeriz 125
    • Story Points:
      3

      Description

      Feature

      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.

      Solution

      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();
      

      EDIT:
      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

        Attachments

          Activity

            People

            • Assignee:
              jsimak Jaroslav Simak
              Reporter:
              cmeier Christoph Meier
            • Votes:
              2 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Date of First Response: