Skip to content

Commit

Permalink
Added support for scope preservation!
Browse files Browse the repository at this point in the history
This is specifically necessary when the inner functions scope will be changed when passed to an other function. Think of map, event handlers, jquery etc.
  • Loading branch information
EnoF committed Apr 18, 2014
1 parent 51fa8f2 commit 86876e4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 22 deletions.
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "enofjs",
"version": "1.1.3",
"version": "1.2.0",
"main": "src/enofjs",
"devDependencies": {
"jasmine": "2.0.0"
"jasmine": "^2.0"
},
"homepage": "http://enof.github.io/EnoFJS/",
"authors": [
Expand Down
44 changes: 24 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
{
"name": "enofjs",
"description": "Enhancing javascript",
"version": "1.1.3",
"version": "1.2.0",
"dependencies": {},
"devDependencies": {
"grunt": "~0.4.1",
"load-grunt-tasks": "~0.1.0",
"grunt-contrib-jshint": "~0.6.0",
"grunt-contrib-watch": "~0.5.2",
"grunt-contrib-uglify": "~0.2.0",
"time-grunt": "~0.1.0",
"grunt-karma": "~0.6.2",
"karma-script-launcher": "~0.1.0",
"karma-chrome-launcher": "~0.1.0",
"karma-firefox-launcher": "~0.1.0",
"karma-html2js-preprocessor": "~0.1.0",
"karma-requirejs": "~0.1.0",
"karma-jasmine": "~0.1.3",
"karma-coffee-preprocessor": "~0.1.0",
"karma-phantomjs-launcher": "~0.1.0",
"karma": "~0.10.2",
"karma-ng-html2js-preprocessor": "~0.1.0",
"karma-coverage": "~0.1.0",
"grunt-groc": "~0.4.5"
"grunt": "^0.4",
"load-grunt-tasks": "^0.1",
"grunt-contrib-jshint": "^0.6",
"grunt-contrib-watch": "^0.5",
"grunt-contrib-uglify": "^0.2",
"time-grunt": "^0.1",
"grunt-karma": "^0.6",
"karma-script-launcher": "^0.1",
"karma-chrome-launcher": "^0.1",
"karma-firefox-launcher": "^0.1",
"karma-html2js-preprocessor": "^0.1",
"karma-requirejs": "^0.1",
"karma-jasmine": "^0.1",
"karma-coffee-preprocessor": "^0.1",
"karma-phantomjs-launcher": "^0.1",
"karma": "^0.10",
"karma-ng-html2js-preprocessor": "^0.1",
"karma-coverage": "^0.1.0",
"grunt-groc": "^0.4"
},
"engines": {
"node": ">=0.8.0"
},
"scripts": {
"test": "grunt test"
},
"repository": {
"type": "git",
"url": "git@github.com:EnoF/EnoFJS.git"
}
}
10 changes: 10 additions & 0 deletions src/ClassFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,16 @@
return functionName;
};

// When an innerfunction will be passed down to an other function, it could be that the scope
// will be modified. Think of event handlers.
// Now you can pass the function with `this.private.getFoo.bind(this)`. This will preserve the scope.
Function.prototype.bindScope = function bindScope(scope) {
var self = this;
return function boundScope() {
return self.apply(scope, arguments);
};
};

// **Getters, Setters and Issers**

// `get || getSet`
Expand Down
10 changes: 10 additions & 0 deletions test/spec/ClassFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,9 @@
this.public = {
getFoo: function getFoo() {
return this.protected.foo;
},
reapply: function reapply() {
this.public.getFoo.bindScope(this).apply({});
}
};
});
Expand Down Expand Up @@ -393,6 +396,13 @@
var child = new Child();
expect(child.getFoo()).toEqual('bar');
});

it('should be able to apply with a different scope', function applyDifferentScope() {
var parent = new Parent();
expect(function testing() {
parent.reapply();
}).not.toThrow();
});
});
});
}());

0 comments on commit 86876e4

Please sign in to comment.