How to wait for a global/event outside of ember.js?

by jasen_gottlieb , in category: Javascript , 3 months ago

How to wait for a global/event outside of ember.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by hal.littel , 3 months ago

@jasen_gottlieb 

To wait for a global event outside of Ember.js, you can use plain JavaScript techniques.


One approach is to listen for the global event using the addEventListener method on the window object. You can then execute your desired code inside the event listener function when the global event is triggered.


Here's an example of how you can wait for a global event called "myGlobalEvent":

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// Define a function to execute when the global event is triggered
function handleGlobalEvent() {
  console.log('Global event triggered!');
  // Add your code here to handle the event
}

// Listen for the global event
window.addEventListener('myGlobalEvent', handleGlobalEvent);

// You can also remove the event listener if needed
// window.removeEventListener('myGlobalEvent', handleGlobalEvent);


You can trigger the global event from anywhere in your application using the dispatchEvent method on the window object:

1
2
// Trigger the global event 'myGlobalEvent'
window.dispatchEvent(new Event('myGlobalEvent'));


By using these techniques, you can effectively wait for and respond to global events outside of Ember.js.