Decorators - jasperjs/jasper-application GitHub Wiki

Overview

Decorator components (decorators) - is a part of application, that allows your to extend existing HTML elements or components. Decorators does not have a template, like components.

Decorators uses as attributes to existing elements and represents in project as class. Instance of the class create on each deorator usage.

To create decorator type:

yo jasper:decorator <AREANAME> <ATTRIBUTE-NAME>

For instance

yo jasper:decorator main focus-on-default

After that Yeoman creates a decorator folder. This folder contains:

  1. _definition.json - stores configuration of the decorator;
  2. DecoratorName.ts - decorator controller.

When you have done - type grunt to build the app.

Configuration

Example:

{
  "type": "decorator",
  "ctrl": "spa.core.decorators.SelectOnFocus",
  "properties": "",
  "events": "selected"

  /* other properties */

}

Configuration may have following properties:

type (required)

For decorators it's allways 'decorator'

ctrl (required)

Specifies the decorator controller.

require

Specifies that this decorator require another component or decorator (almost AngularJS 'require' property). Required components will passed to the controller's 'link' function.

eval (true|false)

Default: true

Legacy api. Please use properties instead.

Specifies need to eval expression, passed to the decorator attribute. Result of the expression will passed to the 'link' function as 'value' parameter.

properties

Properties for decorators works same as for components. Please see Components documentation.

events

Events for decorators works same as for components. Please see Components documentation.

Controller

Decorator controller is a class. It may have the following public methods:

link

Jasper invokes this method when decorator links with HTML element in which his applied.

module spa.core.decorators {
    // somple decorator, which place focus to the applied element
    export class FocusOnDefault {
        // inject services here
        static $inject = [];

        link(value: any, element: HTMLElement, attrs: any, requiredComponents: any) {
            if (element.tagName === "INPUT" || element.tagName === "TEXTAREA") {
                (<HTMLInputElement>element).focus();
            } 
        }

        destroyComponent(){
            //cleanup code here
        }

    }
}

destroyComponent

Jasper invokes this method whem HTML element, associated with decorator, removes from DOM. Place here any cleanup code.

onValueChanged(newValue: any, oldValue: any)

Jasper invokes this method when value passed to decorator element attribute has changed.

When your decorator is done you can use it in your templates, like:

<input type="text" focus-on-default />
⚠️ **GitHub.com Fallback** ⚠️