Component Class for Components which can be used in static page projects in VEAMS
.
The class provides a base system to build components. It merges options, bind and unbind events without worrying about that, subscribe to global events and renders templates with data.
npm install @veams/component --save
yarn add @veams/component
- default options are defined here
- you have to call
super(obj, options)
to merge default with markup options
- called in the constructor
- useful for preparing your component, but cannot use any defined class property
- gets the object and markup options objects as parameters
- This function just executes other functions and will be executed by
@veams/plugin-modules
. - When you want to use a component in another component, call the function after init.
- if needed templates can be pre-rendered here using
renderTemplate
(see section "Render templates")
- bind events manually here using
registerEvent
(see section "Events / Manual binding")
- called as long as option
render
is not set tofalse
in options of module loader - render your templates here or update the state of your component
The VeamsComponent provides useful lifecycle hooks.
- executed after initialize
- executed after initial render
- executed before unregistering events
- executed after unregistering events
- default options defined in constructor will be merged with markup options by calling
super(obj, options)
in constructor - markup options have higher priority and always overwrite default options properties of the same name
Example for overwriting default options with markup options:
<div
data-js-module="slider"
data-js-options='{
"infinite": true,
"pauseOnHover": false}'
>...</div>
When the component gets initialized, it expects to get passed two parameters:
obj
- Generic Object which containsel
- Required: Node/element which is associated with it.namespace
- Optional: Namespace which is associated with the module.options
- Optional: Options object.context
- Optional: The context object is your custom Veams object.
options
- Optional: Markup Options which are read out by this plugin
The Component Class can subscribe to global events using the getter subscribe
.
Global events can be triggered with Veams.Vent.trigger('eventName')
.
Example for global event subscription:
get subscribe() {
return {
'\{{Veams.EVENTS.resize}}': 'onResize'
};
}
Local events can be bound by using the getter events
.
This package has its own event-handler process and is similar to @veams/query
.
That means every event handled by a function can provides to the function two paramaters:
e
- Native event object.target
- Target on which you have clicked.
get events() {
return {
'click': 'onClick'
};
}
In this case the event listener is bound to this.el
and executed as soon as the element itself or any of its children is clicked.
get events() {
return {
'click \{{this.options.specialBtn}}': 'onClick'
};
}
In this case the event listener is also bound to this.el
but only executed when the element is clicked which is predefined as specialBtn
in options.
Sometimes you need to bind events conditionally. Using the provided registerEvent
method is a comfortable way.
Example of manually registering local event in automatically called bindEvents
bindEvents() {
...
if (condition) {
// register global event
this.registerEvent('\{{this.options.globalEvent}}', 'eventHandler', true);
// register local event with event delegation
this.registerEvent('\{{Veams.EVENTS.keydown}} \{{this.options.someSubComponent}}', 'onKeydown');
}
...
}
Rendering templates with VEAMS
is pretty easy. You only have to specify the names of templates you want to use in the options of your component and then make a call to the renderTemplate
function, passing the template name and the data as parameters.
render() {
// render template with provided data
let tmpl = this.renderTemplate('btn', this.data);
// append output to current element
this.$el.append(tmpl);
}
You are able to auto create your component by using the provided method autocreate()
. The method returns the instance and executes all important lifecycle methods for you.
import Component, { autocreate } from '@veams/component';
import Veams from './app.veams'
import Counter from './counter';
const counter = autocreate()(new Counter({
el: document.getElementById('counter')
}));
Veams.onDOMReady(() => {
counter.render();
});
import $ from 'jquery';
import Component from '@veams/component';
class Comparer extends Component {
/**
* Constructor for our class
*
* @see module.js
*
* @param {Object} obj - Object which is passed to our class
* @param {Object} obj.el - element which will be saved in this.el
* @param {Object} obj.options - options which will be passed in as JSON object
*/
constructor(obj) {
let options = {
handle: '[data-js-item="comparer-handle"]',
topContainer: '[data-js-item="comparer-top-container"]',
topContent: '[data-js-item="comparer-top-content"]',
draggClass: 'is-dragging',
dragMode: true,
topContentRight: false,
disabled: {
'desktop': false,
'tablet-large': false,
'tablet-small': false,
'mobile-large': false,
'mobile-medium': false,
'mobile-small': false
}
};
super(obj, options);
this.$el = $(this.el);
}
/** =================================================
* EVENTS
* ================================================ */
/**
* Subscribe to global events of Veams or App namespace.
*/
get subscribe() {
return {
'{{Veams.EVENTS.resize}}': 'preRender',
'{{Veams.EVENTS.mediachange}}': 'render'
};
}
/**
* Bind local events to this.$el.
*/
get events() {
return {
'mousedown {{this.options.handle}}': 'mouseDown',
'mouseup': 'mouseUp',
'touchstart {{this.options.handle}}': 'mouseDown',
'touchend': 'mouseUp',
'mousemove': 'resizeContainer'
};
}
/** =================================================
* STANDARD METHODS
* ================================================= */
/**
* Initialize the view and merge options
*
*/
initialize() {
...
}
/**
* Pre-Render method
*/
preRender(){
...
}
/**
* Render method
*/
render() {
...
}
/** =================================================
* CUSTOM METHODS
* ================================================= */
mouseDown(e) {
...
}
mouseUp() {
...
}
resizeContainer(e) {
...
}
}
export default Comparer;