jeudi 20 août 2015

Ember event on window close

When a user closes the browser, I want to run some code before the window gets closed out, because if not it causes problems for other users. Part of the application I'm working on has a random video chat. The room is created by a user and while they're waiting for someone to join they close their browser tab/window and then the room is not closed out correctly and still open for another user to join.

I've seen many examples using beforeunload, but they just aren't working for me with Ember. Maybe I'm missing a more ember way of doing things, or maybe I need to rely on a heartbeat approach?

What I've been trying to do is use the window beforeunload event, but I haven't been having any luck. If there is a better way to go about solving my above problem, I'm all ears!

Right now I bind the method in the setupController such as:

$(window).bind('beforeunload', function() {
  console.log('ending chat');
  this.endChat();
});

I've also tried this code in the route, and maybe in the view (tried so many things over the past month that I don't remember everything).




Aucun commentaire:

Enregistrer un commentaire