From 7afd0a02e8c60f43f4ab5e9447199c963ee4263e Mon Sep 17 00:00:00 2001 From: lilia Date: Wed, 13 May 2015 11:23:59 -0700 Subject: [PATCH] Convert beforeunload listeners --- js/chromium.js | 8 ++++++++ js/panel_controller.js | 4 ++-- js/views/conversation_list_item_view.js | 2 +- js/views/inbox_view.js | 2 +- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/js/chromium.js b/js/chromium.js index 77be8edc..285986b0 100644 --- a/js/chromium.js +++ b/js/chromium.js @@ -133,6 +133,14 @@ return appWindow.contentWindow; }); } + }, + + beforeUnload: function(callback) { + if (chrome.runtime) { + chrome.runtime.onSuspend.addListener(callback); + } else { + window.addEventListener('beforeunload', callback); + } } }; diff --git a/js/panel_controller.js b/js/panel_controller.js index cb90602e..60f920d6 100644 --- a/js/panel_controller.js +++ b/js/panel_controller.js @@ -92,7 +92,7 @@ windowMap.add({ windowId: windowInfo.id, modelId: modelId }); // close the panel if background.html is refreshed - window.addEventListener('beforeunload', function () { + extension.windows.beforeUnload(function() { // TODO: reattach after reload instead of closing. extension.windows.remove(windowInfo.id); }); @@ -125,7 +125,7 @@ inboxWindowId = windowInfo.id; // close the panel if background.html is refreshed - window.addEventListener('beforeunload', function () { + extension.windows.beforeUnload(function() { // TODO: reattach after reload instead of closing. extension.windows.remove(windowInfo.id); }); diff --git a/js/views/conversation_list_item_view.js b/js/views/conversation_list_item_view.js index 2eba025e..de152b91 100644 --- a/js/views/conversation_list_item_view.js +++ b/js/views/conversation_list_item_view.js @@ -28,7 +28,7 @@ initialize: function() { this.listenTo(this.model, 'change', this.render); // auto update this.listenTo(this.model, 'destroy', this.remove); // auto update - window.addEventListener('beforeunload', function () { + extension.windows.beforeUnload(function() { this.stopListening(); }.bind(this)); }, diff --git a/js/views/inbox_view.js b/js/views/inbox_view.js index a4eec7f2..e6e4d371 100644 --- a/js/views/inbox_view.js +++ b/js/views/inbox_view.js @@ -73,7 +73,7 @@ new SocketView().render().$el.appendTo(this.$('.socket-status')); - window.addEventListener('beforeunload', function () { + extension.windows.beforeUnload(function() { this.inbox.stopListening(); }.bind(this)); },