@area17/a17-behaviors
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -200,3 +200,3 @@ 'use strict'; | ||
this.customMethodNames.forEach(methodName => { | ||
this[methodName] = this[methodName].bind(this); | ||
this[methodName] = this.methods[methodName].bind(this); | ||
}); | ||
@@ -519,7 +519,7 @@ | ||
* @param {string} name - Name of the behavior used for declaration: data-behavior="name" | ||
* @param {BehaviorDef} def - define methods of the behavior | ||
* @param {Lifecycle} lifecycle - Register behavior lifecycle | ||
* @param {object} methods - define methods of the behavior | ||
* @param {object} lifecycle - Register behavior lifecycle | ||
* @returns {Behavior} | ||
*/ | ||
const createBehavior = (name, def, lifecycle = {}) => { | ||
const createBehavior = (name, methods = {}, lifecycle = {}) => { | ||
/** | ||
@@ -548,2 +548,5 @@ * | ||
}, | ||
methods: { | ||
value: methods, | ||
}, | ||
customMethodNames: { | ||
@@ -555,12 +558,9 @@ value: customMethodNames, | ||
// Expose the definition properties as 'this[methodName]' | ||
const defKeys = Object.keys(def); | ||
defKeys.forEach(key => { | ||
const methodsKeys = Object.keys(methods); | ||
methodsKeys.forEach(key => { | ||
customMethodNames.push(key); | ||
customProperties[key] = { | ||
value: def[key], | ||
writable: true, | ||
}; | ||
}); | ||
fn.prototype = Object.create(Behavior.prototype, customProperties); | ||
return fn; | ||
@@ -1196,3 +1196,21 @@ }; | ||
/** | ||
* Extend an existing a behavior instance | ||
* @param {module} behavior - behavior you want to extend | ||
* @param {string} name - Name of the extended behavior used for declaration: data-behavior="name" | ||
* @param {object} methods - define methods of the behavior | ||
* @param {object} lifecycle - Register behavior lifecycle | ||
* @returns {Behavior} | ||
* | ||
* NB: methods or lifestyle fns with the same name will overwrite originals | ||
*/ | ||
function extendBehavior(behavior, name, methods = {}, lifecycle = {}) { | ||
const newMethods = Object.assign(Object.assign({}, behavior.prototype.methods), methods); | ||
const newLifecycle = Object.assign(Object.assign({}, behavior.prototype.lifecycle), lifecycle); | ||
return createBehavior(name, newMethods, newLifecycle); | ||
} | ||
exports.createBehavior = createBehavior; | ||
exports.extendBehavior = extendBehavior; | ||
exports.manageBehaviors = manageBehaviors; |
@@ -198,3 +198,3 @@ var getCurrentMediaQuery = function() { | ||
this.customMethodNames.forEach(methodName => { | ||
this[methodName] = this[methodName].bind(this); | ||
this[methodName] = this.methods[methodName].bind(this); | ||
}); | ||
@@ -517,7 +517,7 @@ | ||
* @param {string} name - Name of the behavior used for declaration: data-behavior="name" | ||
* @param {BehaviorDef} def - define methods of the behavior | ||
* @param {Lifecycle} lifecycle - Register behavior lifecycle | ||
* @param {object} methods - define methods of the behavior | ||
* @param {object} lifecycle - Register behavior lifecycle | ||
* @returns {Behavior} | ||
*/ | ||
const createBehavior = (name, def, lifecycle = {}) => { | ||
const createBehavior = (name, methods = {}, lifecycle = {}) => { | ||
/** | ||
@@ -546,2 +546,5 @@ * | ||
}, | ||
methods: { | ||
value: methods, | ||
}, | ||
customMethodNames: { | ||
@@ -553,12 +556,9 @@ value: customMethodNames, | ||
// Expose the definition properties as 'this[methodName]' | ||
const defKeys = Object.keys(def); | ||
defKeys.forEach(key => { | ||
const methodsKeys = Object.keys(methods); | ||
methodsKeys.forEach(key => { | ||
customMethodNames.push(key); | ||
customProperties[key] = { | ||
value: def[key], | ||
writable: true, | ||
}; | ||
}); | ||
fn.prototype = Object.create(Behavior.prototype, customProperties); | ||
return fn; | ||
@@ -1194,2 +1194,19 @@ }; | ||
export { createBehavior, manageBehaviors }; | ||
/** | ||
* Extend an existing a behavior instance | ||
* @param {module} behavior - behavior you want to extend | ||
* @param {string} name - Name of the extended behavior used for declaration: data-behavior="name" | ||
* @param {object} methods - define methods of the behavior | ||
* @param {object} lifecycle - Register behavior lifecycle | ||
* @returns {Behavior} | ||
* | ||
* NB: methods or lifestyle fns with the same name will overwrite originals | ||
*/ | ||
function extendBehavior(behavior, name, methods = {}, lifecycle = {}) { | ||
const newMethods = Object.assign(Object.assign({}, behavior.prototype.methods), methods); | ||
const newLifecycle = Object.assign(Object.assign({}, behavior.prototype.lifecycle), lifecycle); | ||
return createBehavior(name, newMethods, newLifecycle); | ||
} | ||
export { createBehavior, extendBehavior, manageBehaviors }; |
{ | ||
"name": "@area17/a17-behaviors", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "JavaScript framework to attach JavaScript events and interactions to DOM Nodes", | ||
@@ -5,0 +5,0 @@ "type": "module", |
77073
2219