hplug v1.0.3
hPlug.js
Simple js plugin template.
Futures
Events
- automatic register events around methods(
beforeInit,afterInit, ...);
hPlug uses js CustomEvent that dispatches to main Element(the element on that constructor was called). Event bubbling disabled. On Event details puts instance and arguments for current method:
{
bubbles: false,
detail: {
[this.lowerName]: this,
args: arguments,
},
}(where lowerName - class name with first letter in lower case)
For after event in detail.result also puts value returned by method.
The class implement addEventListener and removeEventListener methods for chain calls support.
Constructors
- register js
Elementand$.fnconstructors for your class(Element.prototype[ClassName],$.fn[this.lowerName]);
After class definition you need to call static method register that register constructors and methods events.
Methods handlers
this.h.methodName- handlerProxythat return method with bindedthisvalue that you can pass in any function/event and can be sure, thatthiswill refer to class object without loss of arguments. Method runned by handler will receive defaultthisobject as first argument andEventobject as second. Example:
Settings proxy
this.s.settingName- settings RecursiveProxy that return SelectorGetter for every string value. see Life cycle.
ResizeTimer
- If you want to subscribe to
window.resizeevent - you can use default hPlug mechanism. You need to implementresizemethod, but as handler for event you will useresizeTimermethod. Timer is set to 250ms by default(you can override it inthis._s.resizeTimout). This timer prevent unnecessary risize calls.
Lifecycle/Extending
In most cases you no need to use constructor in your class. Wjat you need - is implement 2 methods: defs and init.
Class MyPlugin extends hPlug {
// this method exist because of
// ES6 dont allow to define class members in class body
defs() {
this.settings = {
// default setting that will be extended by args
sname1: 'sval1',
sname2: 'sval2',
};
this.myField = 'defaultValue';
}
init() {
// init method has a role of constructor here
// and runs at the end of hPlug constructor
return this;
}
}hPlug constructor and settings
hPlug constructor receive node(Element onthat constructor called) and settings objects.
At the first stage hPlug check if the node already has assigned class object and if it exist - return this object. Otherwise the constructing will continue.
defsmethod will be called.- the node will saved into
this.nodefield:
this.node = jqueryAvailable()
? $(node)
: [node];thiswill be assigned to node:
this.node[0][this.lowerName] = this;Settings arg will be assigned to default values that you define in
defsmethod. Also will be initializedthis.s- RecursiveProxy that reflectthis.settingsobject with one difference - everyStringvalues will be returned as SelectorGetter. So if you need to use pure string value of setting, you can usethis.settings.sname1orthis.s.sname1.s.destructmethod will unsign this object from node and return empty object.
Example
class ContentPlugin extends hPlug {
defs() {
this.node = null;
this.settings = {
sname1: 'sval1',
};
}
init() {
this.node.click(this.h.func);
return this.node[0];
return this;
return {};
}
func(arg) {
console.log(this.s.sname2.s); // 'sval2'
}
}
ContentPlugin.register();
content.on('beforeInit', function(e) {
console.log('beforeInit', arguments);
});
content.on('afterInit', function(e) {
console.log('afterInit', arguments);
});
// jQuery constructor:
var content = $('.content');
var obj = content.contentPlugin({
sname2: 'sval2',
});
console.log(obj[0].contentPlugin); // ContentPlugin {}
// js Element constructor:
var content = document.getElementsByClassName('content');
var obj = content[0].ContentPlugin({
sname2: 'sval2',
});
console.log(obj.contentPlugin); // ContentPlugin {}