Last active
August 29, 2015 14:07
-
-
Save tilmanschweitzer/d5985da95e2df27440dd to your computer and use it in GitHub Desktop.
intercept scope prototype 1.0.x
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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