Skip to content
This repository was archived by the owner on Nov 16, 2019. It is now read-only.

Commit aa370fa

Browse files
committed
fix(Popup): fix memory leak due to detached DOM tree
1 parent fbd1379 commit aa370fa

File tree

7 files changed

+38
-32
lines changed

7 files changed

+38
-32
lines changed

src/popup/app.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,37 @@ define('app', function (require, exports, _module) {
55
var MenuView = require('views/Menu');
66
var CommandsView = require('views/Command');
77
var DomainsView = require('views/Domain');
8+
var cache = require('cache');
89
var tabs = _.require('utils/tabs');
910
var badges = _.require('badges');
1011

1112
var scriptsMenu = exports.scriptsMenu = new Menu;
1213
var commandsMenu = exports.commandsMenu = new Menu;
1314
var domainsMenu = exports.domainsMenu = new Menu;
1415

15-
var App = Backbone.Router.extend({
16+
var App = cache.BaseRouter.extend({
1617
routes: {
1718
'': 'renderMenu',
1819
commands: 'renderCommands',
1920
domains: 'renderDomains',
2021
},
2122
renderMenu: function () {
22-
this.view = new MenuView;
23+
this.loadView('menu', function () {
24+
return new MenuView;
25+
});
2326
},
2427
renderCommands: function () {
25-
this.view = new CommandsView;
28+
this.loadView('commands', function () {
29+
return new CommandsView;
30+
});
2631
},
2732
renderDomains: function () {
28-
this.view = new DomainsView;
33+
this.loadView('domains', function () {
34+
return new DomainsView;
35+
});
2936
},
3037
});
31-
var app = new App();
38+
var app = new App('#app');
3239
if (!Backbone.history.start())
3340
app.navigate('', {trigger: true, replace: true});
3441

src/popup/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<script src="/cache.js"></script>
1313
</head>
1414
<body>
15-
<div id="popup"></div>
15+
<div id="app"></div>
1616
<script src="app.js"></script>
1717
</body>
1818
</html>

src/popup/views/base.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ define('views/Base', function (require, _exports, module) {
55
var badges = _.require('badges');
66

77
module.exports = BaseView.extend({
8-
el: '#popup',
98
templateUrl: '/popup/templates/menu.html',
109
addMenuItem: function (obj, parent) {
1110
if (!(obj instanceof MenuItem)) obj = new MenuItem(obj);
1211
var item = new MenuItemView({model: obj});
1312
parent.append(item.$el);
13+
this.childViews.push(item);
1414
},
1515
components: function () {
1616
var $el = this.$el;
@@ -22,10 +22,12 @@ define('views/Base', function (require, _exports, module) {
2222
};
2323
},
2424
fixStyles: function (div, plh) {
25-
plh.html(div.html());
26-
var pad = div[0].offsetWidth - div[0].clientWidth + 2;
27-
plh.css('padding-right', pad + 'px');
28-
badges.button.popup.height = div.parent().height();
25+
setTimeout(function () {
26+
plh.html(div.html());
27+
var pad = div[0].offsetWidth - div[0].clientWidth + 2;
28+
plh.css('padding-right', pad + 'px');
29+
badges.button.popup.height = div.parent().height();
30+
});
2931
},
3032
});
3133
});

src/popup/views/command.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ define('views/Command', function (require, _exports, module) {
2727
app.commandsMenu.each(function (item) {
2828
_this.addMenuItem(item, bot);
2929
});
30-
setTimeout(function () {
31-
_this.fixStyles(bot, comp.plh);
32-
});
30+
_this.fixStyles(bot, comp.plh);
3331
},
3432
});
3533
});

src/popup/views/domain.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ define('views/Domain', function (require, _exports, module) {
2525
app.domainsMenu.each(function (item) {
2626
_this.addMenuItem(item, bot);
2727
});
28-
setTimeout(function () {
29-
_this.fixStyles(bot, comp.plh);
30-
});
28+
_this.fixStyles(bot, comp.plh);
3129
},
3230
});
3331
});

src/popup/views/item.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,23 @@ define('views/MenuItem', function (require, _exports, module) {
99
'click .menu-item-label': 'onClick',
1010
},
1111
initialize: function () {
12-
BaseView.prototype.initialize.call(this);
13-
this.listenTo(this.model, 'change', this.render);
12+
var _this = this;
13+
BaseView.prototype.initialize.call(_this);
14+
_this.listenTo(_this.model, 'change', _this.render);
1415
},
1516
_render: function () {
16-
var it = this.model.toJSON();
17+
var _this = this;
18+
var it = _this.model.toJSON();
1719
if (typeof it.symbol === 'function')
1820
it.symbol = it.symbol(it.data);
1921
if (typeof it.name === 'function')
2022
it.name = it.name(it.data);
21-
this.$el.html(this.templateFn(it))
23+
_this.$el.html(_this.templateFn(it))
2224
.attr('title', it.title === true ? it.name : it.title);
23-
if (it.data === false) this.$el.addClass('disabled');
24-
else this.$el.removeClass('disabled');
25-
it.className && this.$el.addClass(it.className);
26-
it.onClickDetail && this.$el.addClass('has-detail');
25+
if (it.data === false) _this.$el.addClass('disabled');
26+
else _this.$el.removeClass('disabled');
27+
it.className && _this.$el.addClass(it.className);
28+
it.onClickDetail && _this.$el.addClass('has-detail');
2729
},
2830
onClick: function (e) {
2931
var onClick = this.model.get('onClick');

src/popup/views/menu.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ define('views/Menu', function (require, _exports, module) {
55

66
module.exports = MenuBaseView.extend({
77
initialize: function () {
8-
MenuBaseView.prototype.initialize.call(this);
9-
this.listenTo(app.scriptsMenu, 'reset', this.render);
10-
this.listenTo(app.commandsMenu, 'reset', this.render);
11-
this.listenTo(app.domainsMenu, 'reset', this.render);
8+
var _this = this;
9+
MenuBaseView.prototype.initialize.call(_this);
10+
_this.listenTo(app.scriptsMenu, 'reset', _this.render);
11+
_this.listenTo(app.commandsMenu, 'reset', _this.render);
12+
_this.listenTo(app.domainsMenu, 'reset', _this.render);
1213
},
1314
_render: function () {
1415
var _this = this;
@@ -67,9 +68,7 @@ define('views/Menu', function (require, _exports, module) {
6768
app.scriptsMenu.each(function (item) {
6869
_this.addMenuItem(item, bot);
6970
});
70-
setTimeout(function () {
71-
_this.fixStyles(bot, comp.plh);
72-
});
71+
_this.fixStyles(bot, comp.plh);
7372
},
7473
});
7574
});

0 commit comments

Comments
 (0)