Skip to content

Instantly share code, notes, and snippets.

@rwjblue
Forked from teddyzeenny/gist:9291219
Last active April 18, 2021 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwjblue/9291668 to your computer and use it in GitHub Desktop.
Save rwjblue/9291668 to your computer and use it in GitHub Desktop.
Ember.runLoadHooks = function(name, object) {
loaded[name] = object;
if (typeof window === 'object' && typeof window.dispatchEvent === 'function' && typeof CustomEvent === "function") {
var event = new CustomEvent(name, {detail: object, name: name});
window.dispatchEvent(event);
}
if (loadHooks[name]) {
forEach.call(loadHooks[name], function(callback) {
callback(object);
});
}
};
diff --git a/packages/ember-runtime/lib/system/lazy_load.js b/packages/ember-runtime/lib/system/lazy_load.js
index 3e9285a..4a47a92 100644
--- a/packages/ember-runtime/lib/system/lazy_load.js
+++ b/packages/ember-runtime/lib/system/lazy_load.js
@@ -1,3 +1,5 @@
+/* globals CustomEvent */
+
var forEach = Ember.ArrayPolyfills.forEach;
/**
@@ -49,6 +51,11 @@ Ember.onLoad = function(name, callback) {
Ember.runLoadHooks = function(name, object) {
loaded[name] = object;
+ if (typeof window === 'object' && typeof window.dispatchEvent === 'function' && typeof CustomEvent === "function") {
+ var event = new CustomEvent(name, {detail: object, name: name});
+ window.dispatchEvent(event);
+ }
+
if (loadHooks[name]) {
forEach.call(loadHooks[name], function(callback) {
callback(object);
diff --git a/packages/ember-runtime/tests/system/lazy_load_test.js b/packages/ember-runtime/tests/system/lazy_load_test.js
index 649487f..0edd83d 100644
--- a/packages/ember-runtime/tests/system/lazy_load_test.js
+++ b/packages/ember-runtime/tests/system/lazy_load_test.js
@@ -1,3 +1,5 @@
+/* globals CustomEvent */
+
module("Lazy Loading");
test("if a load hook is registered, it is executed when runLoadHooks are exected", function() {
@@ -49,3 +51,18 @@ test("hooks in ENV.EMBER_LOAD_HOOKS['hookName'] get executed", function() {
equal(window.ENV.__test_hook_count__, 1, "the object was passed into the load hook");
});
+
+if (typeof window === 'object' && typeof window.dispatchEvent === 'function' && typeof CustomEvent === "function") {
+ test("load hooks trigger a custom event", function() {
+ var eventObject = "super duper awesome events";
+
+ window.addEventListener('__test_hook_for_events__', function(e) {
+ ok(true, 'custom event was fired');
+ equal(e.detail, eventObject, 'event details are provided properly');
+ });
+
+ Ember.run(function() {
+ Ember.runLoadHooks("__test_hook_for_events__", eventObject);
+ });
+ });
+}
@github1234567890294
Copy link

e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment