{"action":"created","issue":{"url":"https://api.github.com/repos/rwaldron/johnny-five/issues/1044","repository_url":"https://api.github.com/repos/rwaldron/johnny-five","labels_url":"https://api.github.com/repos/rwaldron/johnny-five/issues/1044/labels{/name}","comments_url":"https://api.github.com/repos/rwaldron/johnny-five/issues/1044/comments","events_url":"https://api.github.com/repos/rwaldron/johnny-five/issues/1044/events","html_url":"https://github.com/rwaldron/johnny-five/issues/1044","id":136392924,"number":1044,"title":"Layer on top of johnny-five - callbacks","user":{"login":"b00682613","id":14835520,"avatar_url":"https://avatars.githubusercontent.com/u/14835520?v=3","gravatar_id":"","url":"https://api.github.com/users/b00682613","html_url":"https://github.com/b00682613","followers_url":"https://api.github.com/users/b00682613/followers","following_url":"https://api.github.com/users/b00682613/following{/other_user}","gists_url":"https://api.github.com/users/b00682613/gists{/gist_id}","starred_url":"https://api.github.com/users/b00682613/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/b00682613/subscriptions","organizations_url":"https://api.github.com/users/b00682613/orgs","repos_url":"https://api.github.com/users/b00682613/repos","events_url":"https://api.github.com/users/b00682613/events{/privacy}","received_events_url":"https://api.github.com/users/b00682613/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-02-25T13:52:13Z","updated_at":"2016-02-26T18:08:13Z","closed_at":null,"body":"@rwaldron @ajfisher How could I pass callbacks in from module.exports to the j5 objects. Take this example:\r\n\r\n```javascript\r\n// my-board.js\r\nvar five = require(\"johnny-five\");\r\nvar board = new five.Board();\r\n\r\nboard.on(\"ready\", function() {\r\n  var motion = new five.IR.Motion(7);\r\n\r\n  motion.on(\"motionstart\", function() {\r\n    console.log(\"motionstart\", Date.now());\r\n  }); // pass callback from module.exports instead\r\n});\r\n\r\nmodule.exports = {\r\n  motionDetected: function (cb) {\r\n     // somehow pass cb into motion.on\r\n     // motion.on('motionstart', cb)\r\n  }\r\n}\r\n\r\n```\r\n\r\n```javascript\r\n// app.js\r\nvar board = require('./my-board')\r\n\r\nboard.motionDetected(function () {\r\n  console.log('Motion detected.')\r\n})\r\n```"},"comment":{"url":"https://api.github.com/repos/rwaldron/johnny-five/issues/comments/189401865","html_url":"https://github.com/rwaldron/johnny-five/issues/1044#issuecomment-189401865","issue_url":"https://api.github.com/repos/rwaldron/johnny-five/issues/1044","id":189401865,"user":{"login":"rwaldron","id":27985,"avatar_url":"https://avatars.githubusercontent.com/u/27985?v=3","gravatar_id":"","url":"https://api.github.com/users/rwaldron","html_url":"https://github.com/rwaldron","followers_url":"https://api.github.com/users/rwaldron/followers","following_url":"https://api.github.com/users/rwaldron/following{/other_user}","gists_url":"https://api.github.com/users/rwaldron/gists{/gist_id}","starred_url":"https://api.github.com/users/rwaldron/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rwaldron/subscriptions","organizations_url":"https://api.github.com/users/rwaldron/orgs","repos_url":"https://api.github.com/users/rwaldron/repos","events_url":"https://api.github.com/users/rwaldron/events{/privacy}","received_events_url":"https://api.github.com/users/rwaldron/received_events","type":"User","site_admin":false},"created_at":"2016-02-26T18:08:13Z","updated_at":"2016-02-26T18:08:13Z","body":"I didn't test it, but this should work: \r\n\r\n```js\r\n// my-board.js\r\nvar five = require(\"johnny-five\");\r\nvar board = new five.Board();\r\nvar callbacks = new Map();\r\n\r\nfunction register(type, callback) {\r\n  if (!callbacks.has(type)) {\r\n    callbacks.set(type, [callback]);\r\n  } else {\r\n    callbacks.get(type).push(callback);\r\n  }\r\n}\r\n\r\nboard.on(\"ready\", function() {\r\n  var motion = new five.IR.Motion(7);\r\n\r\n  motion.on(\"motionstart\", function() {\r\n    console.log(\"motionstart\", Date.now());\r\n    if (callbacks.has(\"motionstart\")) {\r\n      callbacks.get(\"motionstart\").forEach(function() {\r\n        callback();\r\n      });\r\n    }\r\n\r\n  }); // pass callback from module.exports instead\r\n});\r\n\r\nmodule.exports = {\r\n  motionDetected: function (cb) {\r\n    register(\"motionstart\", cb);\r\n  }\r\n}\r\n```"}}