From 4e67879bcc0fbb285666f6130f6259ec9a61044a Mon Sep 17 00:00:00 2001 From: Jackson Tian Date: Mon, 3 Nov 2014 13:38:30 +0800 Subject: [PATCH] Refine the style --- lib/eventproxy.js | 101 ++++++++++++++++++++++++---------------------- 1 file changed, 53 insertions(+), 48 deletions(-) diff --git a/lib/eventproxy.js b/lib/eventproxy.js index 8106e2d..cc9ff2a 100755 --- a/lib/eventproxy.js +++ b/lib/eventproxy.js @@ -1,10 +1,4 @@ /*global define*/ -/*! - * This file is used for define the EventProxy library. - * @author Jackson Tian - * @version 0.2.2 - * ``` - */ !(function (name, definition) { // Check define var hasDefine = typeof define === 'function', @@ -24,7 +18,7 @@ })('EventProxy', function (debug) { debug = debug || function () {}; - /** + /*! * refs */ var SLICE = Array.prototype.slice; @@ -56,7 +50,14 @@ /** * Bind an event, specified by a string name, `ev`, to a `callback` function. * Passing __ALL_EVENT__ will bind the callback to all events fired. - * @param {String} eventName Event name. + * Examples: + * ```js + * var proxy = new EventProxy(); + * proxy.addListener("template", function (event) { + * // TODO + * }); + * ``` + * @param {String} eventname Event name. * @param {Function} callback Callback. */ EventProxy.prototype.addListener = function (ev, callback) { @@ -80,7 +81,7 @@ /** * Bind an event, but put the callback into head of all callbacks. - * @param {String} eventName Event name. + * @param {String} eventname Event name. * @param {Function} callback Callback. */ EventProxy.prototype.headbind = function (ev, callback) { @@ -89,30 +90,31 @@ this._callbacks[ev].unshift(callback); return this; }; + /** * Remove one or many callbacks. * * - If `callback` is null, removes all callbacks for the event. - * - If `eventName` is null, removes all bound callbacks for all events. - * @param {String} eventName Event name. + * - If `eventname` is null, removes all bound callbacks for all events. + * @param {String} eventname Event name. * @param {Function} callback Callback. */ - EventProxy.prototype.removeListener = function (eventName, callback) { + EventProxy.prototype.removeListener = function (eventname, callback) { var calls = this._callbacks; - if (!eventName) { + if (!eventname) { debug('Remove all listeners'); this._callbacks = {}; } else { if (!callback) { - debug('Remove all listeners of %s', eventName); - calls[eventName] = []; + debug('Remove all listeners of %s', eventname); + calls[eventname] = []; } else { - var list = calls[eventName]; + var list = calls[eventname]; if (list) { var l = list.length; for (var i = 0; i < l; i++) { if (callback === list[i]) { - debug('Remove a listener of %s', eventName); + debug('Remove a listener of %s', eventname); list[i] = null; } } @@ -153,21 +155,21 @@ * Trigger an event, firing all bound callbacks. Callbacks are passed the * same arguments as `trigger` is, apart from the event name. * Listening for `"all"` passes the true event name as the first argument. - * @param {String} eventName Event name + * @param {String} eventname Event name * @param {Mix} data Pass in data */ - EventProxy.prototype.trigger = function (eventName, data) { + EventProxy.prototype.trigger = function (eventname, data) { var list, ev, callback, args, i, l; var both = 2; var calls = this._callbacks; - debug('Emit event %s with data %j', eventName, data); + debug('Emit event %s with data %j', eventname, data); while (both--) { - ev = both ? eventName : ALL_EVENT; + ev = both ? eventname : ALL_EVENT; list = calls[ev]; if (list) { for (i = 0, l = list.length; i < l; i++) { if (!(callback = list[i])) { - list.splice(i, 1); + list.splice(i, 1); i--; l--; } else { @@ -179,6 +181,7 @@ } return this; }; + /** * `trigger` alias */ @@ -294,14 +297,15 @@ /** * Assign some events, after all events were fired, the callback will be executed once. + * * Examples: * ```js * proxy.all(ev1, ev2, callback); * proxy.all([ev1, ev2], callback); * proxy.all(ev1, [ev2, ev3], callback); * ``` - * @param {String} eventName1 First event name. - * @param {String} eventName2 Second event name. + * @param {String} eventname1 First event name. + * @param {String} eventname2 Second event name. * @param {Function} callback Callback, that will be called after predefined events were fired. */ EventProxy.prototype.all = function (eventname1, eventname2, callback) { @@ -339,8 +343,8 @@ * proxy.tail([ev1, ev2], callback); * proxy.tail(ev1, [ev2, ev3], callback); * ``` - * @param {String} eventName1 First event name. - * @param {String} eventName2 Second event name. + * @param {String} eventname1 First event name. + * @param {String} eventname2 Second event name. * @param {Function} callback Callback, that will be called after predefined events were fired. */ EventProxy.prototype.tail = function () { @@ -360,11 +364,11 @@ /** * The callback will be executed after the event be fired N times. - * @param {String} eventName Event name. + * @param {String} eventname Event name. * @param {Number} times N times. * @param {Function} callback Callback, that will be called after event was fired N times. */ - EventProxy.prototype.after = function (eventName, times, callback) { + EventProxy.prototype.after = function (eventname, times, callback) { if (times === 0) { callback.call(null, []); return this; @@ -372,18 +376,18 @@ var proxy = this, firedData = []; this._after = this._after || {}; - var group = eventName + '_group'; + var group = eventname + '_group'; this._after[group] = { index: 0, results: [] }; - debug('After emit %s times, event %s\'s listenner will execute', times, eventName); + debug('After emit %s times, event %s\'s listenner will execute', times, eventname); var all = function (name, data) { - if (name === eventName) { + if (name === eventname) { times--; firedData.push(data); if (times < 1) { - debug('Event %s was emit %s, and execute the listenner', eventName, times); + debug('Event %s was emit %s, and execute the listenner', eventname, times); proxy.unbindForAll(all); callback.apply(null, [firedData]); } @@ -392,7 +396,7 @@ times--; proxy._after[group].results[data.index] = data.result; if (times < 1) { - debug('Event %s was emit %s, and execute the listenner', eventName, times); + debug('Event %s was emit %s, and execute the listenner', eventname, times); proxy.unbindForAll(all); callback.call(null, proxy._after[group].results); } @@ -415,12 +419,12 @@ * fs.readFile(files[i], 'utf-8', ep.group('file')); * } * ``` - * @param {String} eventName Event name, shoule keep consistent with `after`. + * @param {String} eventname Event name, shoule keep consistent with `after`. * @param {Function} callback Callback function, should return the final result. */ - EventProxy.prototype.group = function (eventName, callback) { + EventProxy.prototype.group = function (eventname, callback) { var that = this; - var group = eventName + '_group'; + var group = eventname + '_group'; var index = that._after[group].index; that._after[group].index++; return function (err, data) { @@ -438,23 +442,23 @@ /** * The callback will be executed after any registered event was fired. It only executed once. - * @param {String} eventName1 Event name. - * @param {String} eventName2 Event name. - * @param {Function} callback The callback will get a map that has data and eventName attributes. + * @param {String} eventname1 Event name. + * @param {String} eventname2 Event name. + * @param {Function} callback The callback will get a map that has data and eventname attributes. */ EventProxy.prototype.any = function () { var proxy = this, callback = arguments[arguments.length - 1], events = SLICE.call(arguments, 0, -1), - _eventName = events.join("_"); + _eventname = events.join("_"); debug('Add listenner for Any of events %j emit', events); - proxy.once(_eventName, callback); + proxy.once(_eventname, callback); var _bind = function (key) { proxy.bind(key, function (data) { debug('One of events %j emited, execute the listenner'); - proxy.trigger(_eventName, {"data": data, eventName: key}); + proxy.trigger(_eventname, {"data": data, eventname: key}); }); }; @@ -465,15 +469,15 @@ /** * The callback will be executed when the event name not equals with assigned event. - * @param {String} eventName Event name. + * @param {String} eventname Event name. * @param {Function} callback Callback. */ - EventProxy.prototype.not = function (eventName, callback) { + EventProxy.prototype.not = function (eventname, callback) { var proxy = this; - debug('Add listenner for not event %s', eventName); + debug('Add listenner for not event %s', eventname); proxy.bindForAll(function (name, data) { - if (name !== eventName) { - debug('listenner execute of event %s emit, but not event %s.', name, eventName); + if (name !== eventname) { + debug('listenner execute of event %s emit, but not event %s.', name, eventname); callback(data); } }); @@ -552,6 +556,7 @@ /** * make done async + * @return {Function} delay done */ EventProxy.prototype.doneLater = function (handler, callback) { var _doneHandler = this.done(handler, callback); @@ -576,7 +581,7 @@ * // do something... * }); * ``` - * @returns {EventProxy} EventProxy instance + * @return {EventProxy} EventProxy instance */ EventProxy.create = function () { var ep = new EventProxy();