Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Work around bug in sub() where namespaced data isnt cleaned by cleanD…

…ata resulting in memory leak
  • Loading branch information...
commit ac22410cd0e847e9383384b971eca6cfd744de1d 1 parent 233cd24
@hafriedlander authored
Showing with 31 additions and 0 deletions.
  1. +28 −0 spec/spec.entwine.namespaces.js
  2. +3 −0  src/jquery.entwine.js
View
28 spec/spec.entwine.namespaces.js
@@ -270,6 +270,34 @@ describe('Entwine', function(){
expect(res).toEqual([1, 3]);
});
+ it('properly clears data in namespace', function(){
+ var res = [];
+
+ $('#a').entwine('foo', function($){return{
+ foo: function(){ this.data('foo', 'foo'); }
+ };});
+
+ // Add the data, then check it's there
+ $('#a').entwine('foo').foo();
+ expect($('#a').data('foo')).toEqual('foo');
+
+ // Check cache is shared between root & namespace
+ var rootInitialCacheKeys = $.map($.cache, function(v, k){ return k; });
+ var namespaceInitialCacheKeys = $.map($.entwine.namespaces['foo'].$.cache, function(v, k){ return k; });
+
+ expect(rootInitialCacheKeys).toEqual(namespaceInitialCacheKeys);
+
+ // Remove '#a', and check cache is still shared
+ $('#a').remove();
+ var rootEventualCacheKeys = $.map($.cache, function(v, k){ return k; });
+ var namespaceEventualCacheKeys = $.map($.entwine.namespaces['foo'].$.cache, function(v, k){ return k; });
+
+ expect(rootEventualCacheKeys).toEqual(namespaceEventualCacheKeys);
+
+ // And that it's now smaller that it used to be
+ expect(namespaceEventualCacheKeys.length).toBeLessThan(namespaceInitialCacheKeys.length);
+
+ });
});
});
View
3  src/jquery.entwine.js
@@ -149,6 +149,9 @@ catch (e) {
else {
// We're in a namespace, so we build a Class that subclasses the jQuery Object Class to inject namespace functions into
this.$ = $.sub ? $.sub() : sub();
+ // Work around bug in sub() - subclass must share cache with root or data won't get cleared by cleanData
+ this.$.cache = $.cache;
+
this.injectee = this.$.prototype;
// We override entwine to inject the name of this namespace when defining blocks inside this namespace

0 comments on commit ac22410

Please sign in to comment.
Something went wrong with that request. Please try again.