[$scope.$watchMulti] added unwatch return function

This commit is contained in:
Spencer Alger 2015-04-27 13:38:47 -07:00
parent 881ef7e86f
commit d57f41e00c
2 changed files with 33 additions and 5 deletions

View file

@ -31,7 +31,7 @@ define(function (require) {
*
* @param {array[string|function|obj]} expressions - the list of expressions to $watch
* @param {Function} fn - the callback function
* @return {undefined}
* @return {Function} - an unwatch function, just like the return value of $watch
*/
$delegate.constructor.prototype.$watchMulti = function (expressions, fn) {
if (!_.isArray(expressions)) throw new TypeError('expected an array of expressions to watch');
@ -43,11 +43,11 @@ define(function (require) {
var fire = false;
// first, register all of the multi-watchers
expressions.forEach(function (expr, i) {
var unwatchers = expressions.map(function (expr, i) {
expr = normalizeExpression($scope, expr);
if (!expr) return;
expr.fn.call($scope, expr.get, function (newVal, oldVal) {
return expr.fn.call($scope, expr.get, function (newVal, oldVal) {
vals[i] = newVal;
prev[i] = oldVal;
fire = true;
@ -57,7 +57,7 @@ define(function (require) {
// then, the watcher that checks to see if any of
// the other watchers triggered this cycle
var flip = false;
$scope.$watch(function () {
unwatchers.push($scope.$watch(function () {
if (fire) {
fire = false;
flip = !flip;
@ -68,7 +68,9 @@ define(function (require) {
vals.forEach(function (v, i) {
prev[i] = v;
});
});
}));
return _.partial(_.callEach, unwatchers);
};
function normalizeExpression($scope, expr) {

View file

@ -99,5 +99,31 @@ define(function (require) {
$rootScope.$apply();
expect(count).to.be(2);
});
it('returns a working unwatch function', function () {
$scope.a = 0;
$scope.b = 0;
var triggers = 0;
var unwatch = $scope.$watchMulti(['a', 'b'], function () { triggers++; });
// initial watch
$scope.$apply();
expect(triggers).to.be(1);
// prove that it triggers on chagne
$scope.a++;
$scope.$apply();
expect(triggers).to.be(2);
// remove watchers
expect($scope.$$watchers).to.not.eql([]);
unwatch();
expect($scope.$$watchers).to.eql([]);
// prove that it doesn't trigger anymore
$scope.a++;
$scope.$apply();
expect(triggers).to.be(2);
});
});
});