Skip to content

Instantly share code, notes, and snippets.

@savokiss
Created December 31, 2017 14:28
Show Gist options
  • Save savokiss/3e6143f84c121d99dea3711eb53fb479 to your computer and use it in GitHub Desktop.
Save savokiss/3e6143f84c121d99dea3711eb53fb479 to your computer and use it in GitHub Desktop.
import Class from './class'
var Event = Class({
initialize : function() {
this.__event = window.Zepto && window.Zepto.Events ? new window.Zepto.Events : $({});
}
});
var proto = Event.prototype;
['bind', 'one'].forEach(function(method) {
proto[method] = function(type, handler, context) {
if($.isPlainObject(type)){
for(var i in type)
this[method](i, type[i]);
}else{
var event = this.__event;
var callback = function() {
return handler.apply(context || event, arguments.length > 0 ? (window.Zepto ? arguments : Array.prototype.slice.call(arguments, 1)) : []);
};
event[method].call(event, type, callback);
handler.guid = callback.guid;
}
return this;
};
});
['unbind', 'trigger', 'triggerHandler'].forEach(function(method) {
proto[method] = function() {
var event = this.__event;
// if (require.debug) {
// console.log('[event] ' + this.constructor.__mid + ' : ' + arguments[0], arguments[1]);
// }
var ret = event[method].apply(event, arguments);
// if (require.debug && ret != event && ret != undefined) {
// console.log(ret);
// }
return ret;
};
});
proto.fire = proto.trigger;
proto.firing = proto.triggerHandler;
Event.mix = function(receiver) {
return $.extend(receiver, new Event());
};
export default Event;
// WEBPACK FOOTER //
// ./src/utility/event.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment