Sign up (with export icon)

Template

Api-class icon class

A basic Template class. It renders a DOM HTML element or text from a definition and supports element attributes, children, bindings to observables and DOM event propagation.

A simple template can look like this:

const bind = Template.bind( observable, emitter );

new Template( {
	tag: 'p',
	attributes: {
		class: 'foo',
		style: {
			backgroundColor: 'yellow'
		}
	},
	on: {
		click: bind.to( 'clicked' )
	},
	children: [
		'A paragraph.'
	]
} ).render();
Copy code

and it will render the following HTML element:

<p class="foo" style="background-color: yellow;">A paragraph.</p>
Copy code

Additionally, the observable will always fire clicked upon clicking <p> in the DOM.

See TemplateDefinition to know more about templates and complex template definitions.

Properties

Methods

  • Chevron-right icon

    constructor( def )

    Creates an instance of the Template class.

    Parameters

    def : TemplateDefinition

    The definition of the template.

  • Chevron-right icon

    apply( node ) → HTMLElement | Text

    Applies the template to an existing DOM Node, either HTML element or text.

    Note: No new DOM nodes will be created. Applying extends:

    attributes, event listeners, and textContent of children only.

    Note: Existing class and style attributes are extended when a template is applied to an HTML element, while other attributes and textContent are overridden.

    Note: The process of applying a template can be easily reverted using the revert method.

    const element = document.createElement( 'div' );
    const observable = new Model( { divClass: 'my-div' } );
    const emitter = Object.create( EmitterMixin );
    const bind = Template.bind( observable, emitter );
    
    new Template( {
    	attributes: {
    		id: 'first-div',
    		class: bind.to( 'divClass' )
    	},
    	on: {
    		click: bind( 'elementClicked' ) // Will be fired by the observable.
    	},
    	children: [
    		'Div text.'
    	]
    } ).apply( element );
    
    console.log( element.outerHTML ); // -> '<div id="first-div" class="my-div"></div>'
    
    Copy code

    Parameters

    node : HTMLElement | Text

    Root node for the template to apply.

    Returns

    HTMLElement | Text

    Related:

  • Chevron-right icon

    delegate( events ) → EmitterMixinDelegateChain
    inherited

    Delegates selected events to another Emitter. For instance:

    emitterA.delegate( 'eventX' ).to( emitterB );
    emitterA.delegate( 'eventX', 'eventY' ).to( emitterC );
    
    Copy code

    then eventX is delegated (fired by) emitterB and emitterC along with data:

    emitterA.fire( 'eventX', data );
    
    Copy code

    and eventY is delegated (fired by) emitterC along with data:

    emitterA.fire( 'eventY', data );
    
    Copy code

    Parameters

    events : Array<string>

    Event names that will be delegated to another emitter.

    Returns

    EmitterMixinDelegateChain
  • Chevron-right icon

    fire( eventOrInfo, args ) → GetEventInfo<TEvent>[ 'return' ]
    inherited

    Fires an event, executing all callbacks registered for it.

    The first parameter passed to callbacks is an EventInfo object, followed by the optional args provided in the fire() method call.

    Type parameters

    TEvent : extends BaseEvent

    The type describing the event. See BaseEvent.

    Parameters

    eventOrInfo : GetNameOrEventInfo<TEvent>

    The name of the event or EventInfo object if event is delegated.

    args : TEvent[ 'args' ]

    Additional arguments to be passed to the callbacks.

    Returns

    GetEventInfo<TEvent>[ 'return' ]

    By default the method returns undefined. However, the return value can be changed by listeners through modification of the evt.return's property (the event info is the first param of every callback).

  • Chevron-right icon

    getViews() → IterableIterator<View<HTMLElement>>

    Returns an iterator which traverses the template in search of View instances and returns them one by one.

    const viewFoo = new View();
    const viewBar = new View();
    const viewBaz = new View();
    const template = new Template( {
    	tag: 'div',
    	children: [
    		viewFoo,
    		{
    			tag: 'div',
    			children: [
    				viewBar
    			]
    		},
    		viewBaz
    	]
    } );
    
    // Logs: viewFoo, viewBar, viewBaz
    for ( const view of template.getViews() ) {
    	console.log( view );
    }
    
    Copy code

    Returns

    IterableIterator<View<HTMLElement>>
  • Chevron-right icon

    listenTo( emitter, event, callback, [ options ] ) → void
    inherited

    Registers a callback function to be executed when an event is fired in a specific (emitter) object.

    Events can be grouped in namespaces using :. When namespaced event is fired, it additionally fires all callbacks for that namespace.

    // myEmitter.on( ... ) is a shorthand for myEmitter.listenTo( myEmitter, ... ).
    myEmitter.on( 'myGroup', genericCallback );
    myEmitter.on( 'myGroup:myEvent', specificCallback );
    
    // genericCallback is fired.
    myEmitter.fire( 'myGroup' );
    // both genericCallback and specificCallback are fired.
    myEmitter.fire( 'myGroup:myEvent' );
    // genericCallback is fired even though there are no callbacks for "foo".
    myEmitter.fire( 'myGroup:foo' );
    
    Copy code

    An event callback can stop the event and set the return value of the fire method.

    Type parameters

    TEvent : extends BaseEvent

    The type describing the event. See BaseEvent.

    Parameters

    emitter : Emitter

    The object that fires the event.

    event : TEvent[ 'name' ]

    The name of the event.

    callback : GetCallback<TEvent>

    The function to be called on event.

    [ options ] : GetCallbackOptions<TEvent>

    Additional options.

    Returns

    void
  • Chevron-right icon

    off( event, callback ) → void
    inherited

    Stops executing the callback on the given event. Shorthand for this.stopListening( this, event, callback ).

    Parameters

    event : string

    The name of the event.

    callback : Function

    The function to stop being called.

    Returns

    void
  • Chevron-right icon

    on( event, callback, [ options ] ) → void
    inherited

    Registers a callback function to be executed when an event is fired.

    Shorthand for this.listenTo( this, event, callback, options ) (it makes the emitter listen on itself).

    Type parameters

    TEvent : extends BaseEvent

    The type descibing the event. See BaseEvent.

    Parameters

    event : TEvent[ 'name' ]

    The name of the event.

    callback : GetCallback<TEvent>

    The function to be called on event.

    [ options ] : GetCallbackOptions<TEvent>

    Additional options.

    Returns

    void
  • Chevron-right icon

    once( event, callback, [ options ] ) → void
    inherited

    Registers a callback function to be executed on the next time the event is fired only. This is similar to calling on followed by off in the callback.

    Type parameters

    TEvent : extends BaseEvent

    The type descibing the event. See BaseEvent.

    Parameters

    event : TEvent[ 'name' ]

    The name of the event.

    callback : GetCallback<TEvent>

    The function to be called on event.

    [ options ] : GetCallbackOptions<TEvent>

    Additional options.

    Returns

    void
  • Chevron-right icon

    render() → HTMLElement | Text

    Renders a DOM Node (an HTML element or text) out of the template.

    const domNode = new Template( { ... } ).render();
    
    Copy code

    See: apply.

    Returns

    HTMLElement | Text
  • Chevron-right icon

    revert( node ) → void

    Reverts a template applied to a DOM node.

    Parameters

    node : HTMLElement | Text

    The root node for the template to revert. In most of the cases, it is the same node used by apply.

    Returns

    void
  • Chevron-right icon

    stopDelegating( [ event ], [ emitter ] ) → void
    inherited

    Stops delegating events. It can be used at different levels:

    • To stop delegating all events.
    • To stop delegating a specific event to all emitters.
    • To stop delegating a specific event to a specific emitter.

    Parameters

    [ event ] : string

    The name of the event to stop delegating. If omitted, stops it all delegations.

    [ emitter ] : Emitter

    (requires event) The object to stop delegating a particular event to. If omitted, stops delegation of event to all emitters.

    Returns

    void
  • Chevron-right icon

    stopListening( [ emitter ], [ event ], [ callback ] ) → void
    inherited

    Stops listening for events. It can be used at different levels:

    • To stop listening to a specific callback.
    • To stop listening to a specific event.
    • To stop listening to all events fired by a specific object.
    • To stop listening to all events fired by all objects.

    Parameters

    [ emitter ] : Emitter

    The object to stop listening to. If omitted, stops it for all objects.

    [ event ] : string

    (Requires the emitter) The name of the event to stop listening to. If omitted, stops it for all events from emitter.

    [ callback ] : Function

    (Requires the event) The function to be removed from the call list for the given event.

    Returns

    void
  • Chevron-right icon

    _bindToObservable( options = { options.data, options.schema, options.updater } ) → void
    Lock icon private

    For a given TemplateValueSchema containing TemplateBinding activates the binding and sets its initial value.

    Note: TemplateValueSchema can be for HTML element attributes or text node textContent.

    Parameters

    options : object

    Binding options.

    Properties
    options.data : RenderData

    Rendering data.

    options.schema : Array<TemplateSimpleValue | TemplateBinding>
    options.updater : Updater

    A function which updates the DOM (like attribute or text).

    Returns

    void
  • Chevron-right icon

    _renderAttributes( data ) → void
    Lock icon private

    Renders HTML element attributes out of attributes.

    Parameters

    data : RenderData

    Rendering data.

    Returns

    void
  • Chevron-right icon

    _renderElement( data ) → HTMLElement | Text
    Lock icon private

    Renders an HTML element out of the template.

    Parameters

    data : RenderData

    Rendering data.

    Returns

    HTMLElement | Text
  • Chevron-right icon

    _renderElementChildren( data ) → void
    Lock icon private

    Recursively renders HTML element's children from children.

    Parameters

    data : RenderData

    Rendering data.

    Returns

    void
  • Chevron-right icon

    _renderNode( data ) → HTMLElement | Text
    Lock icon private

    Renders a DOM Node (either an HTML element or text) out of the template.

    Parameters

    data : RenderData

    Rendering data.

    Returns

    HTMLElement | Text
  • Chevron-right icon

    _renderStyleAttribute( styles, data ) → void
    Lock icon private

    Renders the style attribute of an HTML element based on attributes.

    A style attribute is an object with static values:

    attributes: {
    	style: {
    		color: 'red'
    	}
    }
    
    Copy code

    or values bound to UIModel properties:

    attributes: {
    	style: {
    		color: bind.to( ... )
    	}
    }
    
    Copy code

    Note: The style attribute is rendered without setting the namespace. It does not seem to be needed.

    Parameters

    styles : Record<string, TemplateSimpleValue | TemplateBinding>

    Styles located in attributes.style of TemplateDefinition.

    data : RenderData

    Rendering data.

    Returns

    void
  • Chevron-right icon

    _renderText( data ) → HTMLElement | Text
    Lock icon private

    Renders a text node out of text.

    Parameters

    data : RenderData

    Rendering data.

    Returns

    HTMLElement | Text
  • Chevron-right icon

    _revertTemplateFromNode( node, revertData ) → void
    Lock icon private

    Reverts template data from a node to return it to the original state.

    Parameters

    node : HTMLElement | Text

    A node to be reverted.

    revertData : RevertData

    An object that stores information about what changes have been made by apply to the node. See revertData for more information.

    Returns

    void
  • Chevron-right icon

    _setUpListeners( data ) → void
    Lock icon private

    Activates on event listeners from the TemplateDefinition on an HTML element.

    Parameters

    data : RenderData

    Rendering data.

    Returns

    void

Static methods

  • Chevron-right icon

    bind( observable, emitter ) → BindChain<TObservable>
    static

    An entry point to the interface which binds DOM nodes to observables. There are two types of bindings:

    • HTML element attributes or text textContent synchronized with attributes of an Observable. Learn more about to and if.
    const bind = Template.bind( observable, emitter );
    
    new Template( {
    	attributes: {
    		// Binds the element "class" attribute to observable#classAttribute.
    		class: bind.to( 'classAttribute' )
    	}
    } ).render();
    
    Copy code
    • DOM events fired on HTML element propagated through Observable. Learn more about to.
    const bind = Template.bind( observable, emitter );
    
    new Template( {
    	on: {
    		// Will be fired by the observable.
    		click: bind( 'elementClicked' )
    	}
    } ).render();
    
    Copy code

    Also see bindTemplate.

    Type parameters

    TObservable : extends Observable<TObservable>

    Parameters

    observable : TObservable

    An observable which provides boundable attributes.

    emitter : Emitter

    An emitter that listens to observable attribute changes or DOM Events (depending on the kind of the binding). Usually, a View instance.

    Returns

    BindChain<TObservable>
  • Chevron-right icon

    extend( template, def ) → void
    static

    Extends an existing Template instance with some additional content from another TemplateDefinition.

    const bind = Template.bind( observable, emitter );
    
    const template = new Template( {
    	tag: 'p',
    	attributes: {
    		class: 'a',
    		data-x: bind.to( 'foo' )
    	},
    	children: [
    		{
    			tag: 'span',
    			attributes: {
    				class: 'b'
    			},
    			children: [
    				'Span'
    			]
    		}
    	]
     } );
    
    // Instance-level extension.
    Template.extend( template, {
    	attributes: {
    		class: 'b',
    		data-x: bind.to( 'bar' )
    	},
    	children: [
    		{
    			attributes: {
    				class: 'c'
    			}
    		}
    	]
    } );
    
    // Child extension.
    Template.extend( template.children[ 0 ], {
    	attributes: {
    		class: 'd'
    	}
    } );
    
    Copy code

    the outerHTML of template.render() is:

    <p class="a b" data-x="{ observable.foo } { observable.bar }">
    	<span class="b c d">Span</span>
    </p>
    
    Copy code

    Parameters

    template : Template

    An existing template instance to be extended.

    def : Partial<TemplateDefinition>

    Additional definition to be applied to a template.

    Returns

    void