Introduction
The Smart.BaseElement class extends the native HTMLElement class adding additional features like Data Binding, Cross-Browser and Device compatible event system, Localization, Property Change notifications, Typed properties, Templates, Lifecycle callbacks and additional useful properties, methods and events.
Lifecycle callbacks
- created - Called when the element has been created, but before property values are set and local DOM is initialized.
Use for one-time set-up before property values are set. - attached - Called after the element is attached to the document. Can be called multiple times during the lifetime of an element.
- ready - Called when the element is ready. Use for one-time configuration of your element.
- detached - Called after the element is detached from the document. Can be called multiple times during the lifetime of an element.
Properties
To add properties on your custom element, you can use the properties object. All properties part of the properties object are automatically serialized and deserialized by the element and can also be set through attributes by using the dash(-) syntax in the HTML markup. Each property can have the following members:
- reflectToAttribute - Type: Boolean. Set to true to cause the corresponding attribute to be set on the host node when the property value changes. If the property value is Boolean, the attribute is created as a standard HTML boolean attribute (set if true, not set if false). For other property types, the attribute value is a string representation of the property value. The default value of this member is true.
- defaultReflectToAttribute - Type: Boolean. Set to true when we want a default attribute value to be set on the host node.
- readOnly - Type: Boolean. Determines whether the property is readyonly. if true the property can’t be set by the user.
- type - Type: String. Used for deserializing from an attribute.
- any - allows assigning any value to a property.
- string - allows assigning a String to a property.
- string? - allows assigning a ‘String’ or null to a property.
- boolean or bool - allows assigning a Boolean to a property.
- boolean? or bool? - allows assigning a ‘Boolean’ or null to a property.
- number or float - allows assigning a ‘Number’ to a property.
- number? or float? - allows assigning a ‘Number’ or null to a property.
- int or integer - allows assigning an ‘Integer’ to a property.
- int? or integer? - allows assigning an ‘Integer’ or null to a property.
- date - allows assigning a ‘Date’ to a property.
- date? - allows assigning a ‘Date’ or null to a property.
- array - allows assigning an ‘Array’ to a property.
- object - allows assigning an ‘Object’ to a property.
- allowedValues - Type: Array. Used for defining a set of values which are allowed to be set. For other values, an exception is thrown.
- notify - Type: Boolean. Determines whether an event is raised when a property is changed. The event name is: property’s attribute name + - ‘changed’. Example: Property’s name is ‘clickMode’, the event’s name will be ‘click-mode-changed’.
- value - Default value for the property.
- observer - Type: String. A name of a function called within the Element when the property is changed. The arguments passed to your observer are the property’s oldValue and newValue.
- validator - Type: String. A name of a function called within the Element when the property is changing. The arguments passed to your validator are the property’s oldValue and newValue. The function returns the updated value. If it returns undefined, the newValue remains unchanged.
propertyChangedHandler(propertyName, oldValue, newValue) method is called when a property is changed by the user. This method is useful for updating the element when the user makes some changes.
The user may watch for property changes by using the element’s instance. watch(propertiesArray, propertyChangedCallback). The arguments passed to the propertyChangedCallback function are propertyName, oldValue, newValue.
Template
The template object determines the internal HTML structure of the Element. Within that structure you can data bind properties by using two-way or one-way data binding.
template() { return '<button class=\'smart-button\' inner-h-t-m-l=\'[[innerHTML]]\' id=\'button\' type=\'[[type]]\' name=\'[[name]]\' value=\'[[value]]\' disabled=\'[[disabled]]\' role=\'button\'></button>'; }
Text surrounded by double curly bracket ({{ }}) or double square bracket ([[ ]]) delimiters. Identifies the host element’s property being bound.
- Double-curly brackets (}) is used for two-way data flow.
- Double square brackets ([[ ]]) is used for one-way downward from host element to target element data flow.
Two-way binding to a Native HTML element.
nativeElementProperty="{{hostElementProperty::nativeElementEvent}}"
A set of utility functions is accessible throught the $ symbol. The syntax is element.$.The utilify functions are:
- addClass(className) - adds a class or classes to the element separated by space.
- removeClass(className) - removes a class or classes separated by space.
- isNativeElement - returns true if the element is native HTML Element. Otherwise returns false.
- fireEvent(eventType, detail, options) - fires a Custom Event.
- eventType - String. Determines the event’s type.
- detail - Object. Determines custom event object passed to the user.
- options - Object. Determines the event’s options like cancelable or bubbles. Read more on: https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent
- listen(eventType, handler) - adds an event listener to the element. A set of Mobile-friendly events are supported by default. By passing any of these event types: down, up, move, tap, taphold, swipeleft, swiperight, swipetop, swipebottom, you will be notified when the user taps, swipes or touches with finger or mouse the element. If you listen to the resize event, you will be notified whenever the element’s boundaries are changed.
- unlisten(eventType) - removes event listener by type.
- getAttributeValue(attributeName, type) - gets the attribute’s typed value.
- setAttributeValue(attributeName, value, type) - sets the attribute’s value by using a typed value.
By invoking Smart.Utilities.Extend(element) you can extend any element with the above utility functions.
In order to add a custom utility class, you can use Smart.Utilities.Assign(classDefinition).
Smart.Utilities.Assign('BaseNumericProcessor', class BaseNumericProcessor { }
To access that class, you can use Smart.Utilities.BaseNumericProcessor.