Fundamentals - Cheat Sheet VI

Custom Elements

Custom Element View-Model Declaration
import {customElement, bindable} from 'aurelia-framework'; 

@customElement('say-hello')  
export class SayHello { 
           @bindable to; 
           
           speak(){  
                    alert(`Hello ${this.to}!`); 
           }
}

Custom Element View Declaration
<template> 
        <button click.trigger="speak()">Say Hello To ${to}</button> 
</template>

Custom Element Use
<template>  
        <require from="say-hello"></require>  
        <input type="text" ref="name"> <say-hello to.bind="name.value"></say-hello>  
</template>

Custom Element Without View-Model Declaration

Declare Custom Element Without View-Model With Binding
<template bindable="greeting,name"> 
                 Say ${greeting} To ${name}  
</template>  

Add Global Custom Element Without View-Model
aurelia.use.globalResources('./js-less-component.html');  

Use Custom Element Without View-Model
<require from="./js-less-component.html"></require> 
<js-less-component greeting="Hello" name.bind="someProperty"></js-less-component>

Custom Element Variable Binding

-  when binding variables to custom elements, use camelCase inside the custom element's View-Model, and dash-case on the html element

Custom Element View-Model Declaration
import {bindable} from 'aurelia-framework'; 

export class SayHello { 
           @bindable to; 
           @bindable greetingCallback; 

           speak(){ 
                    this.greetingCallback(`Hello ${this.to}!`); 
           } 
}

Custom Element Use
<template>  
        <require from="./say-hello"></require> 
        <input type="text" ref="name">  
        <say-hello to.bind="name.value" greeting-callback.call="doSomething($event)"></say-hello> </template>

Custom Element Options

@children(selector) - Decorates a property to create an array on your class that has its items automatically synchronized based on a query selector against the element's immediate child content.
@child(selector) - Decorates a property to create a reference to a single immediate child content element.
@processContent(false|Function) - Tells the compiler that the element's content requires special processing. If you provide false to the decorator, the compiler will not process the content of your custom element. It is expected that you will do custom processing yourself. But, you can also supply a custom function that lets you process the content during the view's compilation. That function can then return true/false to indicate whether or not the compiler should also process the content. The function takes the following form function(compiler, resources, node, instruction):boolean
@useView(path) - Specifies a different view to use.
@noView() - Indicates that this custom element does not have a view and that the author intends for the element to handle its own rendering internally.
@inlineView(markup, dependencies?) - Allows the developer to provide a string that will be compiled into the view.
@useShadowDOM() - Causes the view to be rendered in the ShadowDOM. When an element is rendered to ShadowDOM, a special DOMBoundary instance can optionally be injected into the constructor. This represents the shadow root.
@containerless() - Causes the element's view to be rendered without the custom element container wrapping it. This cannot be used in conjunction with @child, @children or @useShadowDOM decorators. It also cannot be uses with surrogate behaviors. Use sparingly. 

SVG Elements

- SVG (scalable vector graphic) tags can support Aurelia's custom element <template> tags by nesting the templated code inside a second <svg> tag.
- For example if you had a base <svg> element and wanted to add a templated <rect> inside it, you would first put your custom tag inside the main <svg> tag. Also, make sure the custom element class uses the @containerless() decorator.


SVG Custom Element View-Model Declaration
import {containerless} from 'aurelia-framework'; 

@containerless() 
export class MyCustomRect {
  ... 
}

SVG Custom Element View
<template>  
        <svg> <rect width="10" height="10" fill="red" x="50" y="50"/> </svg> 
</template>

SVG Custom Element Use
<template> 
        <require from="my-custom-rect"></require> 
        <svg width="100" height="100" > 
                 <my-custom-rect></my-custom-rect>  
        </svg> 
</template>

Template Parts

Custom Element View with Replaceable Parts
<template> 
        <ul>  
             <li class="foo" repeat.for="item of items"> 
                  <template replaceable part="item-template"> Original: ${item} </template> 
             </li> 
        <ul> 
</template>

Custom Element Use with Replacement
<template>  
        <require from="my-element"></require> 
        <my-element>  
                 <template replace-part="item-template"> Replacement: ${item} </template> 
        </my-element> 
</template>

Surrogate Behavior Use
<template role="progress-bar" aria-valuenow.bind="progress" aria-valuemin="0" aria-valuemax="100"> 
        <div class="bar"> 
                 <div class="progress" css="width:${progress}%"></div> 
        </div> 
</template>

Observable decorator

- Aurelia exposes a decorator named observable to allow watching for changes to a property and reacting to them. 
- By convention it will look for a matching method name Changed -  
- Correct Observable Usage
import {observable} from 'aurelia-framework';  

export class MyCustomViewModel { 
           @observable name = 'Hello world';  
           nameChanged(newValue, oldValue) { 
                    // react to change 
          } 
}
-  The developer can also specify a different method name to use -
Correct observable usage with configured change handler
import {observable} from 'aurelia-framework';  

export class MyCustomViewModel { 
           @observable({changeHandler: 'nameChanged'}) name = 'Hello world'; 
            nameChanged(newValue, oldValue) {  
                     // react to change 
           }
}

Comments

Popular posts from this blog

Fundamentals - App Config & Startup - VIII

Fundamentals - Dependency Injection V

Style vs Css attribute