{"_id":"dispatchy","_rev":"146793","name":"dispatchy","description":"A jquery like event emitter/dispatcher that could be mixed with other objects to provide emitting capabilities ala jQuery Style","dist-tags":{"latest":"1.0.3"},"maintainers":[{"name":"royriojas","email":"royriojas@gmail.com"}],"time":{"modified":"2021-06-03T11:30:10.000Z","created":"2015-03-09T09:33:19.608Z","1.0.3":"2015-07-07T08:47:39.733Z","1.0.2":"2015-03-09T09:49:30.536Z","1.0.1":"2015-03-09T09:36:24.601Z","1.0.0":"2015-03-09T09:33:19.608Z"},"users":{},"author":{"name":"Roy Riojas"},"repository":{"type":"git","url":"https://github.com/royriojas/dispatchy.git"},"versions":{"1.0.3":{"name":"dispatchy","version":"1.0.3","description":"A jquery like event emitter/dispatcher that could be mixed with other objects to provide emitting capabilities ala jQuery Style","main":"index.js","scripts":{"beautify":"esbeautifier './index.js' './specs/**/*.js'","beautify-check":"esbeautifier -k './index.js' './specs/**/*.js'","eslint":"eslinter './index.js' './specs/**/*.js'","lint":"npm run beautify && npm run eslint","test":"mocha-runner './specs/**/*.js'","cover":"istanbul cover -x specs/**/*.js mocha-runner './specs/**/*.js' html text-summary","watch":"watch-spawn -i -p npm run cover","check":"npm run beautify-check && npm run eslint","prepush":"npm run check && npm run test","do-changelog":"changelogx -f markdown -o ./changelog.md && git add ./changelog.md && git commit -m 'DOC: Generate Changelog'","install-hooks":"prepush install && changelogx install-hook","bump":"bumpery --verify='npm run check'"},"repository":{"type":"git","url":"https://github.com/royriojas/dispatchy.git"},"keywords":["jQuery","custom","events","jquery","eventEmitter","eventEmitter","eventDispatcher","dispatcher"],"author":{"name":"Roy Riojas"},"license":"MIT","bugs":{"url":"https://github.com/royriojas/dispatchy/issues"},"homepage":"https://github.com/royriojas/dispatchy","dependencies":{"is-null-like":"^1.0.2"},"devDependencies":{"bumpery":"^1.0.7","chai":"^1.10.0","chai-fuzzy":"^1.4.0","changelogx":"^1.0.13","esbeautifier":"^3.1.3","eslint":"^0.16.1","eslint-friendly-formatter":"^1.0.3","eslinter":"^2.0.1","istanbul":"^0.3.17","lodash.random":"^3.0.0","mocha":"^2.2.1","mocha-runner":"^1.0.8","prepush":"^2.0.4","proxyquire":"^1.6.0","sinon":"^1.13.0","sinon-chai":"^2.7.0","underscore":"^1.8.2","watch-spawn":"^1.0.3"},"gitHead":"aceaccd581b195c0e66eca8758ddd59cd7bf95c0","_id":"dispatchy@1.0.3","_shasum":"bf97c499657d4dfdb929ee1169647038529076c6","_from":".","_npmVersion":"2.6.0","_nodeVersion":"0.12.0","_npmUser":{"name":"royriojas","email":"royriojas@gmail.com"},"maintainers":[{"name":"royriojas","email":"royriojas@gmail.com"}],"dist":{"shasum":"bf97c499657d4dfdb929ee1169647038529076c6","size":8825,"noattachment":false,"key":"/dispatchy/-/dispatchy-1.0.3.tgz","tarball":"http://registry.cnpm.dingdandao.com/dispatchy/download/dispatchy-1.0.3.tgz"},"directories":{},"publish_time":1436258859733,"_cnpm_publish_time":1436258859733,"_hasShrinkwrap":false},"1.0.2":{"name":"dispatchy","version":"1.0.2","description":"A jquery like event emitter/dispatcher that could be mixed with other objects to provide emitting capabilities ala jQuery Style","main":"index.js","scripts":{"lint":"eslint --format 'node_modules/eslint-friendly-formatter' index.js specs/  -c './deps/eslint.json'","test":"mocha-runner 'specs/**/*.js'","cover":"istanbul cover -x specs/**/*.js mocha-runner 'specs/**/*.js' html text-summary","watch":"npm run cover && watch-spawn -p 'specs/**/*.js' -p 'index.js' npm run cover"},"repository":{"type":"git","url":"https://github.com/royriojas/dispatchy.git"},"keywords":["jQuery","custom","events","jquery","eventEmitter","eventEmitter","eventDispatcher","dispatcher"],"author":{"name":"Roy Riojas"},"license":"MIT","bugs":{"url":"https://github.com/royriojas/dispatchy/issues"},"homepage":"https://github.com/royriojas/dispatchy","dependencies":{"lodash.isnull":"^3.0.0","lodash.isobject":"^3.0.0","lodash.trim":"^3.0.1"},"devDependencies":{"chai":"^1.10.0","chai-fuzzy":"^1.4.0","eslint":"^0.16.1","eslint-friendly-formatter":"^1.0.3","istanbul":"^0.3.7","lodash.random":"^3.0.0","mocha":"^2.2.1","mocha-runner":"^1.0.1","proxyquire":"^1.4.0","sinon":"^1.13.0","sinon-chai":"^2.7.0","underscore":"^1.8.2","watch-spawn":"^1.0.3"},"gitHead":"aa9dc81958be0412de1f416341fcf981d0ecebaf","_id":"dispatchy@1.0.2","_shasum":"28b3dbbca90aad0d85415533432b5a9509f99d73","_from":".","_npmVersion":"2.6.0","_nodeVersion":"0.12.0","_npmUser":{"name":"royriojas","email":"royriojas@gmail.com"},"maintainers":[{"name":"royriojas","email":"royriojas@gmail.com"}],"dist":{"shasum":"28b3dbbca90aad0d85415533432b5a9509f99d73","size":13015,"noattachment":false,"key":"/dispatchy/-/dispatchy-1.0.2.tgz","tarball":"http://registry.cnpm.dingdandao.com/dispatchy/download/dispatchy-1.0.2.tgz"},"directories":{},"publish_time":1425894570536,"_cnpm_publish_time":1425894570536,"_hasShrinkwrap":false},"1.0.1":{"name":"dispatchy","version":"1.0.1","description":"A jquery like event emitter/dispatcher that could be mixed with other objects to provide emitting capabilities ala jQuery Style","main":"index.js","scripts":{"lint":"eslint --format 'node_modules/eslint-friendly-formatter' index.js specs/  -c './deps/eslint.json'","test":"mocha-runner 'specs/**/*.js'","cover":"istanbul cover -x specs/**/*.js mocha-runner 'specs/**/*.js' html text-summary","watch":"npm run cover && watch-spawn -p 'specs/**/*.js' -p 'index.js' npm run cover"},"repository":{"type":"git","url":"https://github.com/royriojas/dispatchy.git"},"keywords":["jQuery","custom","events","jquery","eventEmitter","eventEmitter","eventDispatcher","dispatcher"],"author":{"name":"Roy Riojas"},"license":"MIT","bugs":{"url":"https://github.com/royriojas/dispatchy/issues"},"homepage":"https://github.com/royriojas/dispatchy","dependencies":{"lodash.isnull":"^3.0.0","lodash.isobject":"^3.0.0","lodash.trim":"^3.0.1"},"devDependencies":{"chai":"^1.10.0","chai-fuzzy":"^1.4.0","eslint":"^0.16.1","eslint-friendly-formatter":"^1.0.3","istanbul":"^0.3.7","lodash.random":"^3.0.0","mocha":"^2.2.1","mocha-runner":"^1.0.1","proxyquire":"^1.4.0","sinon":"^1.13.0","sinon-chai":"^2.7.0","underscore":"^1.8.2","watch-spawn":"^1.0.3"},"gitHead":"9a74db7a25af8eefa47f811fdcc0f68b7b02b380","_id":"dispatchy@1.0.1","_shasum":"3d22c68bf97740faa0dbb6f6033bad6b96d5bd7a","_from":".","_npmVersion":"2.6.0","_nodeVersion":"0.12.0","_npmUser":{"name":"royriojas","email":"royriojas@gmail.com"},"maintainers":[{"name":"royriojas","email":"royriojas@gmail.com"}],"dist":{"shasum":"3d22c68bf97740faa0dbb6f6033bad6b96d5bd7a","size":13078,"noattachment":false,"key":"/dispatchy/-/dispatchy-1.0.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/dispatchy/download/dispatchy-1.0.1.tgz"},"directories":{},"publish_time":1425893784601,"_cnpm_publish_time":1425893784601,"_hasShrinkwrap":false},"1.0.0":{"name":"dispatchy","version":"1.0.0","description":"A jquery like event emitter/dispatcher that could be mixed with other objects to provide emitting capabilities ala jQuery Style","main":"index.js","scripts":{"lint":"eslint --format 'node_modules/eslint-friendly-formatter' index.js specs/  -c './deps/eslint.json'","test":"mocha-runner 'specs/**/*.js'","cover":"istanbul cover -x specs/**/*.js mocha-runner 'specs/**/*.js' html text-summary","watch":"npm run cover && watch-spawn -p 'specs/**/*.js' -p 'index.js' npm run cover"},"repository":{"type":"git","url":"https://github.com/royriojas/dispatchy.git"},"keywords":["jQuery","custom","events","jquery","eventEmitter","eventEmitter","eventDispatcher","dispatcher"],"author":{"name":"Roy Riojas"},"license":"MIT","bugs":{"url":"https://github.com/royriojas/dispatchy/issues"},"homepage":"https://github.com/royriojas/dispatchy","dependencies":{"lodash.isnull":"^3.0.0","lodash.isobject":"^3.0.0","lodash.trim":"^3.0.1"},"devDependencies":{"eslint":"^0.16.1","eslint-friendly-formatter":"^1.0.3","istanbul":"^0.3.7","lodash.random":"^3.0.0","mocha-runner":"^1.0.1","proxyquire":"^1.4.0","watch-spawn":"^1.0.3"},"gitHead":"5b8ad3c67047d0d311bb4ca25fa5fa2355819f8d","_id":"dispatchy@1.0.0","_shasum":"0c5bccd5c4f775a62e8c0dc4f3eb0d07f179ce88","_from":".","_npmVersion":"2.6.0","_nodeVersion":"0.12.0","_npmUser":{"name":"royriojas","email":"royriojas@gmail.com"},"maintainers":[{"name":"royriojas","email":"royriojas@gmail.com"}],"dist":{"shasum":"0c5bccd5c4f775a62e8c0dc4f3eb0d07f179ce88","size":13322,"noattachment":false,"key":"/dispatchy/-/dispatchy-1.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/dispatchy/download/dispatchy-1.0.0.tgz"},"directories":{},"publish_time":1425893599608,"_cnpm_publish_time":1425893599608,"_hasShrinkwrap":false}},"readme":"[![NPM Version](http://img.shields.io/npm/v/dispatchy.svg?style=flat)](https://npmjs.org/package/dispatchy)\n[![Build Status](http://img.shields.io/travis/royriojas/dispatchy.svg?style=flat)](https://travis-ci.org/royriojas/dispatchy)\n\n# dispatchy\nA jquery like event emitter/dispatcher that could be mixed with other objects to provide emitting capabilities ala jQuery Style\n\n## Install\n\n```bash\nnpm i --save dispatchy\n```\n\n## Usage\n\n`dispatchy` implements the same interface of the jquery events related methods, but for any object.\n\n## Example\n\n```javascript \nvar dispatchy = require('dispatchy');\n\nvar myObject = dispatchy.create(); // will return a newly fresh dispatchy instance for your use and pleasure\n\n// now you can do\n\nmyObject.on('some:event', function (e, args) {\n  // do something when the event is fired\n  console.log('received', args.some);\n});\n\nlater you can\n\nmyObject.fire('some:event', {\n  some: 'data'\n});\n\n//and you will see in the console\n// received data.\n```\n\n## API\n\n**The main methods are:**\n\n- **on**: add event listeners to the dispatcher instance\n  ```javascript\n  var obj = {};\n  var dispatchy = require('dispatchy');\n  var extend = require(lodash.merge);\n  \n  extend(obj, dispatchy);\n  \n  obj.on('some:event', function(e, args) {\n    console.log('handler called for ', e, args);\n  });\n  \n  obj.on('some:event another:event', function(e, args) {\n    console.log('handler called for ', e, args);\n  });\n  \n  obj.on('some:event.namespace1 another:event.namespace2', function (e, args) {\n    console.log('handler called for ', e, args);\n  });\n  \n  obj.on( {\n    'item:action' : function (e, args) {\n      console.log('handler called for', e, args);\n     },\n    'another:action.withNS some:other:here.too': function (e, args) {\n      console.log('handler called for', e, args);\n    }\n  } );\n  ```\n\n- **off**: remove event listeners from the dispatcher instance\n  ```javascript\n  var fn = function(e, args) {\n        console.log('handler called for ', e, args);\n      };\n  // registering the listener\n  obj.on('some:event', fn);\n  \n  // removing it\n  obj.off('some:event', fn);\n  \n  // registering the listener\n  obj.on('another:event.withNamespace', function (e) {});\n  \n  // removing it using the namespace. No need to pass the listener\n  obj.off('another:event.withNamespace');\n  \n  // registering the listeners\n  obj.on('another:event.withNamespace', function (e) {});\n  obj.on('justonemore:event.withNamespace', function (e) {});\n  \n  // removing all events with the given namespace. handy to remove all events with the same namespace\n  obj.off('.withNamespace');\n  ```\n\n- **one**: add an event to the dispatcher instance that will be removed after its execution\n  ```javascript\n  var obj = {};\n  var dispatchy = require('dispatchy');\n  var extend = require(lodash.merge);\n  \n  extend(obj, dispatchy);\n  \n  obj.one('some:event', function(e, args) {\n    console.log('handler called for ', e, args);\n  });\n  \n  obj.one('some:event another:event', function(e, args) {\n    console.log('handler called for ', e, args);\n  });\n  \n  obj.one('some:event.namespace1 another:event.namespace2', function (e, args) {\n    console.log('handler called for ', e, args);\n  });\n  \n  obj.one( {\n    'item:action' : function (e, args) {\n      console.log('handler called for', e, args);\n     },\n    'another:action.withNS some:other:here.too': function (e, args) {\n      console.log('handler called for', e, args);\n    }\n  } );\n  ```\n- **fire**: fires an event, executing all the listeners added to the particular fired event,\n  ```javascript\n  var fn = function(e, args) {\n    console.log('handler called for ', e, args);\n  };\n  // registering the listener\n  obj.on('some:event', fn);\n  \n  // executing it\n  obj.fire('some:event');\n  \n  // registering other listener\n  obj.on('some:event.withNamespace', function (e) {});\n  \n  // this will fire the event with the given namespace if found. won't try to fire some:event.\n  obj.fire('some:event.withNamespace');\n  \n  // execute all events of the given namespace\n  obj.fire('.withNamespace');\n  ```\n\n**Other methods:** This methods are not like the ones in jQuery but can be used to achieve something like\n`special events` on the dispatchers. \n\n- **registerEvent**: Register an event with a given lifecycle, similar to [jQuery Special Events](http://learn.jquery.com/events/event-extensions/#setup-function-data-object-namespaces-eventhandle-function)\n  ```javascript\n  var customEventLifeCycle = {\n    // executed the first time a listener is added for the given event\n    setup: function () {},\n    // executed every time an event listener is added\n    add: function () {},\n    // executed every time an event listener is removed\n    remove: function () {},\n    // execute when the last listener is about to be removed from the dispatcher\n    teardown: function () {}\n  };\n  \n  var dispatchy = require('dispatchy').create();\n  dispatchy.registerEvent('app:ready', customLifeCycle);\n  \n  ```\n\n- **registerPersistentEvent**: For a lack of a better name this method register a special event that once is fired\n  Any listeners added after will be immediately executed. Is like the jQuery `ready` event.\n  ```javascript\n  var dispatchy = require('dispatchy');\n  dispatchy.registerPersistentEvent('app:ready');\n  \n  dispatchy.fire('app:ready', { some: 'data' }); // no event listeners added yet.. no problem\n  \n  // later in the code\n  \n  dispatchy.on('app:ready', function (e, args) {\n    // this will be executed immediately, the args passed to the original event \n    // are also passed to this one so in this case the args will be\n    // { some: 'data' } \n  });\n  ```","_attachments":{},"homepage":"https://github.com/royriojas/dispatchy","bugs":{"url":"https://github.com/royriojas/dispatchy/issues"},"license":"MIT"}