events-mixin
Advanced tools
Comparing version 1.2.0 to 1.3.0
79
index.js
@@ -64,4 +64,13 @@ | ||
* | ||
* @param {String} event | ||
* @param {String|function} [method] | ||
* Multiple events handling: | ||
* | ||
* events.bind({ | ||
* 'click .remove': 'remove', | ||
* 'click .add': 'add' | ||
* }); | ||
* | ||
* @param {String|object} - object is used for multiple binding, | ||
* string for single event binding | ||
* @param {String|function} [arg2] - method to call (optional) | ||
* @param {*} [arg3] - data for single event binding (optional) | ||
* @return {Function} callback | ||
@@ -71,37 +80,49 @@ * @api public | ||
Events.prototype.bind = function(event, method){ | ||
var e = parse(event); | ||
var el = this.el; | ||
var obj = this.obj; | ||
var name = e.name; | ||
var method = method || 'on' + name; | ||
var args = [].slice.call(arguments, 2); | ||
Events.prototype.bind = function(arg1, arg2){ | ||
var bindEvent = function(event, method) { | ||
var e = parse(event); | ||
var el = this.el; | ||
var obj = this.obj; | ||
var name = e.name; | ||
var method = method || 'on' + name; | ||
var args = [].slice.call(arguments, 2); | ||
// callback | ||
function cb(){ | ||
var a = [].slice.call(arguments).concat(args); | ||
// callback | ||
function cb(){ | ||
var a = [].slice.call(arguments).concat(args); | ||
if (typeof method === 'function') { | ||
method.apply(obj, a); | ||
return; | ||
if (typeof method === 'function') { | ||
method.apply(obj, a); | ||
return; | ||
} | ||
if (!obj[method]) { | ||
throw new Error(method + ' method is not defined'); | ||
} else { | ||
obj[method].apply(obj, a); | ||
} | ||
} | ||
if (!obj[method]) { | ||
throw new Error(method + ' method is not defined'); | ||
// bind | ||
if (e.selector) { | ||
cb = delegate.bind(el, e.selector, name, cb); | ||
} else { | ||
obj[method].apply(obj, a); | ||
events.bind(el, name, cb); | ||
} | ||
} | ||
// bind | ||
if (e.selector) { | ||
cb = delegate.bind(el, e.selector, name, cb); | ||
// subscription for unbinding | ||
this.sub(name, method, cb); | ||
return cb; | ||
}; | ||
if (typeof arg1 == 'string') { | ||
bindEvent.apply(this, arguments); | ||
} else { | ||
events.bind(el, name, cb); | ||
for(var key in arg1) { | ||
if (arg1.hasOwnProperty(key)) { | ||
bindEvent.call(this, key, arg1[key]); | ||
} | ||
} | ||
} | ||
// subscription for unbinding | ||
this.sub(name, method, cb); | ||
return cb; | ||
}; | ||
@@ -108,0 +129,0 @@ |
{ | ||
"name": "events-mixin", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"scripts": { | ||
"build": "browserify -r ./index.js:events > build/build.js" | ||
}, | ||
"component": { | ||
@@ -5,0 +8,0 @@ "scripts": { |
@@ -99,2 +99,12 @@ | ||
There is also a shorthand syntax | ||
for binding multiple events with one call: | ||
```js | ||
events.bind({ | ||
'click .remove': 'remove', | ||
'click .close': 'hide' | ||
}); | ||
``` | ||
### Events.unbind([event], [method]) | ||
@@ -101,0 +111,0 @@ |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
11410
11
196
125
1