Skip to content

Instantly share code, notes, and snippets.

@tilmanschweitzer
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tilmanschweitzer/d5985da95e2df27440dd to your computer and use it in GitHub Desktop.
Save tilmanschweitzer/d5985da95e2df27440dd to your computer and use it in GitHub Desktop.
intercept scope prototype 1.0.x
function interceptFunction (object, fnName, options) {
var noop = function () {};
var fnToWrap = object[fnName];
var before = options.before || noop;
var after = options.after || noop;
object[fnName] = function () {
before.apply(this, arguments);
var result = fnToWrap.apply(this, arguments);
after.apply(this, arguments);
return result
}
}
var $scope = angular.element(document.querySelector("[ng-app]")).scope();
var scopePrototype = Object.getPrototypeOf($scope.$root);
var counterPrototype = {
count: 0,
increase: function(n) {
this.report();
this.count = this.count + (n || 1);
},
reset: function (n) {
this.report();
this.count = (n || 0);
},
report: function () {
console.log("digestCounter.count: " + this.count);
}
};
var digestCounter = Object.create(counterPrototype);
interceptFunction(scopePrototype, "$digest", {
before: digestCounter.increase.bind(digestCounter)
});
// check
digestCounter.reset();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment