[NPMCLI-137] Create JavaScript model for page and component template optionally Created: 07/Aug/17  Updated: 14/Feb/18  Resolved: 07/Dec/17

Status: Closed
Project: Magnolia CLI
Component/s: None
Affects Version/s: None
Fix Version/s: 3.0.0

Type: New Feature Priority: Neutral
Reporter: Christoph Meier Assignee: Jaroslav Simak
Resolution: Fixed Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Template:
Acceptance criteria:
Empty
Date of First Response:
Epic Link: CLI v3
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



 Comments   
Comment by Tomáš Gregovský [ 07/Aug/17 ]

yes! yes! yes!

Generated at Mon Feb 12 04:46:53 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.