{"action":"created","issue":{"url":"https://api.github.com/repos/aframevr/aframe/issues/705","repository_url":"https://api.github.com/repos/aframevr/aframe","labels_url":"https://api.github.com/repos/aframevr/aframe/issues/705/labels{/name}","comments_url":"https://api.github.com/repos/aframevr/aframe/issues/705/comments","events_url":"https://api.github.com/repos/aframevr/aframe/issues/705/events","html_url":"https://github.com/aframevr/aframe/issues/705","id":126481538,"number":705,"title":"Forgo `hasLoaded` in favor of a `when` method to detect when an event has been fired at least once","user":{"login":"ngokevin","id":674727,"avatar_url":"https://avatars.githubusercontent.com/u/674727?v=3","gravatar_id":"","url":"https://api.github.com/users/ngokevin","html_url":"https://github.com/ngokevin","followers_url":"https://api.github.com/users/ngokevin/followers","following_url":"https://api.github.com/users/ngokevin/following{/other_user}","gists_url":"https://api.github.com/users/ngokevin/gists{/gist_id}","starred_url":"https://api.github.com/users/ngokevin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ngokevin/subscriptions","organizations_url":"https://api.github.com/users/ngokevin/orgs","repos_url":"https://api.github.com/users/ngokevin/repos","events_url":"https://api.github.com/users/ngokevin/events{/privacy}","received_events_url":"https://api.github.com/users/ngokevin/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/aframevr/aframe/labels/feature%20request","name":"feature request","color":"bfe5bf"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2016-01-13T18:23:38Z","updated_at":"2016-03-10T19:27:47Z","closed_at":null,"body":"<a href=\"https://github.com/cvan\"><img src=\"https://avatars.githubusercontent.com/u/203725?v=3\" align=\"left\" width=\"96\" height=\"96\" hspace=\"10\"></img></a> **Issue by [cvan](https://github.com/cvan)**\n_Thursday Dec 03, 2015 at 09:21 GMT_\n_Originally opened as https://github.com/aframevr/aframe-core/issues/585_\n\n----\n\nmy browser crashed midway through typing a comment [here](https://github.com/aframevr/aframe-editor/commit/042d064#diff-806fed3130056adbc98407610072d4cfR20) (and I guess GitHub's `localStorage` recovery didn't work in this case), so I'll paste a screenshot of the part of the comment I could recover:\r\n\r\n<img width=\"752\" alt=\"screenshot 2015-12-03 00 58 11\" src=\"https://cloud.githubusercontent.com/assets/203725/11556010/f21a3792-9958-11e5-9899-f6bf0c5f0570.png\">\r\n\r\nit all started in PR #330. I initially created a method `ANode.prototype.on`, and @dmarcos and I made the realisation that if your reference to the element (in this example: `this.scene`) is not yet a registered custom element with the new custom prototype and instead still has a `HTMLElement` prototype, the `on` method won't exist. :cry: times.\r\n\r\npossible workarounds I've thought of:\r\n\r\n1. override the native `addEventListener` method (only for any element that extends `ANode`, obviously) so that when the `this.scene.addEventListener` gets called first (i.e., \"registered\"), the listener callback function would get called immediately if we knew that particular event had already been fired internally by us (via `emit`). (I dislike this approach/hack the most.)\r\n2. Extend `HTMLElement`'s prototype to add an `on` method. (This is the approach @dmarcos and I thought we'd try, and [I did it in the PR](https://github.com/MozVR/aframe-core/pull/330/files#diff-151c7cdafeca2782a05a053c10a06156R63), but I decided to merge against it. I dislike this a lot; it's bad practice to pollute the prototype of native nodes - likely resulting in side-effects galore.)\r\n3. Expose some special listener that we could listen to and users could listen to in their JS. (In my original version of PR #330, I had code everywhere do `utils.on(this.scene, 'loaded', function () {})`. It's not great, but it solves the problem. Ideally, if we decide to tackle this again (and we'll likely need to since I've already had to do this exact same thing in the [Pano Explorer](https://github.com/MozVR/aframe-core/blob/ddf77ca/examples/panoexplorer/index.html#L98-L102)), we should simply expose a method like `when` like so (could even get fancy and do something with Promises, but seems unnecessary):\r\n\r\n```js\r\n// Whenever the event gets loaded at least once.\r\naframe.when('loaded', this.scene, function () {});\r\naframe.when('loaded', 'a-scene', function () {});\r\n\r\n// And the generic cases for *adding event listeners* (à la jQuery): `on`, `off,` `once`.\r\naframe.on('loaded', this.scene, function () {});\r\naframe.once('loaded', this.scene, function () {});\r\naframe.off('loaded', this.scene, function () {});\r\n```\r\n\n"},"comment":{"url":"https://api.github.com/repos/aframevr/aframe/issues/comments/195009994","html_url":"https://github.com/aframevr/aframe/issues/705#issuecomment-195009994","issue_url":"https://api.github.com/repos/aframevr/aframe/issues/705","id":195009994,"user":{"login":"ngokevin","id":674727,"avatar_url":"https://avatars.githubusercontent.com/u/674727?v=3","gravatar_id":"","url":"https://api.github.com/users/ngokevin","html_url":"https://github.com/ngokevin","followers_url":"https://api.github.com/users/ngokevin/followers","following_url":"https://api.github.com/users/ngokevin/following{/other_user}","gists_url":"https://api.github.com/users/ngokevin/gists{/gist_id}","starred_url":"https://api.github.com/users/ngokevin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ngokevin/subscriptions","organizations_url":"https://api.github.com/users/ngokevin/orgs","repos_url":"https://api.github.com/users/ngokevin/repos","events_url":"https://api.github.com/users/ngokevin/events{/privacy}","received_events_url":"https://api.github.com/users/ngokevin/received_events","type":"User","site_admin":false},"created_at":"2016-03-10T19:27:47Z","updated_at":"2016-03-10T19:27:47Z","body":"Going to mark this as optional for 0.2.0 since it's an enhancement, but I understand how important and useful it is. It will be a priority."}}