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

Commit 330ed91

Browse files
committed
Merge pull request #42 from C2FO/fix-memory-leak
Fix memory leak with event binding
2 parents 51d8710 + 9cfeded commit 330ed91

File tree

6 files changed

+25
-25
lines changed

6 files changed

+25
-25
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "thumbs",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"main": "thumbs.js",
55
"author": "Pollenware",
66
"ignore": [

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "thumbs",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"private": true,
55
"author": "Pollenware",
66
"license": "MIT",

src/thumbs.view.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Thumbs.View = (function () {
6666
this.__identifiers = [];
6767
this.__monitors = {};
6868
this.__events = {};
69+
this.events = _.clone(this.events || {});
6970
this._super('initialize', arguments);
7071
if (this.$el) {
7172
this.$el.attr("thumbs-id", this.thumbsId);
@@ -384,16 +385,11 @@ Thumbs.View = (function () {
384385
},
385386

386387
checkForEvents: function () {
387-
var self = this,
388-
hasEvents = false;
389-
this.events = this.events || {};
388+
var self = this;
390389

391390
this.$('[data-thumbs-delegate]').each(function() {
392-
hasEvents = self._bindEvents(this) || hasEvents;
391+
self._bindEvents(this);
393392
});
394-
if (hasEvents) {
395-
this.delegateEvents();
396-
}
397393
return this;
398394
},
399395

@@ -431,6 +427,10 @@ Thumbs.View = (function () {
431427
.findEl()
432428
.assign(this._subviews)
433429
._super('render', arguments);
430+
431+
if (!_.isEmpty(this.events)) {
432+
this.delegateEvents();
433+
}
434434
return this;
435435
},
436436

test/spec/delegate.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ describe("Thumbs.View event", function () {
2121
});
2222

2323
it("adds events set in the template to the events object", function () {
24-
expect(view.events).not.toBeDefined();
24+
expect(view.events).toEqual({});
2525
view.render();
26-
expect(view.events).toBeDefined();
26+
expect(_.keys(view.events).length).toEqual(1);
2727
});
2828

2929
it("allows events to be set directly on an element", function () {
@@ -55,9 +55,9 @@ describe("Thumbs.View event", function () {
5555
});
5656

5757
it("adds events set in the template to the events object", function () {
58-
expect(view.events).not.toBeDefined();
58+
expect(view.events).toEqual({});
5959
view.render();
60-
expect(view.events).toBeDefined();
60+
expect(_.keys(view.events).length).toEqual(2);
6161
});
6262

6363
it("allows events to be set directly on an element", function () {

thumbs.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Thumbs.js 0.2.0
1+
// Thumbs.js 0.2.1
22
//
3-
// Copyright (c) 2013 Pollenware.
3+
// Copyright (c) 2014 Pollenware.
44
// Distributed under MIT license.
55
//
66
// http://thumbsjs.com
@@ -359,6 +359,7 @@
359359
this.__identifiers = [];
360360
this.__monitors = {};
361361
this.__events = {};
362+
this.events = _.clone(this.events || {});
362363
this._super('initialize', arguments);
363364
if (this.$el) {
364365
this.$el.attr("thumbs-id", this.thumbsId);
@@ -677,16 +678,11 @@
677678
},
678679

679680
checkForEvents: function () {
680-
var self = this,
681-
hasEvents = false;
682-
this.events = this.events || {};
681+
var self = this;
683682

684683
this.$('[data-thumbs-delegate]').each(function() {
685-
hasEvents = self._bindEvents(this) || hasEvents;
684+
self._bindEvents(this);
686685
});
687-
if (hasEvents) {
688-
this.delegateEvents();
689-
}
690686
return this;
691687
},
692688

@@ -724,6 +720,10 @@
724720
.findEl()
725721
.assign(this._subviews)
726722
._super('render', arguments);
723+
724+
if (!_.isEmpty(this.events)) {
725+
this.delegateEvents();
726+
}
727727
return this;
728728
},
729729

thumbs.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)