Merge remote-tracking branch 'origin/open717' into open-master

This commit is contained in:
bwyu
2015-02-02 16:58:27 -08:00
6 changed files with 164 additions and 5 deletions

View File

@@ -35,7 +35,8 @@ define(
// Link; start listening for changes to an element's size
function link(scope, element, attrs) {
var lastBounds;
var lastBounds,
active = true;
// Determine how long to wait before the next update
function currentInterval() {
@@ -62,9 +63,19 @@ define(
width: element[0].offsetWidth,
height: element[0].offsetHeight
});
$timeout(onInterval, currentInterval());
if (active) {
$timeout(onInterval, currentInterval());
}
}
// Stop running in the background
function deactivate() {
active = false;
}
// Unregister once out-of-scope
scope.$on("$destroy", deactivate);
// Handle the initial callback
onInterval();
}

View File

@@ -14,7 +14,7 @@ define(
beforeEach(function () {
mockTimeout = jasmine.createSpy("$timeout");
mockScope = jasmine.createSpyObj("$scope", ["$eval"]);
mockScope = jasmine.createSpyObj("$scope", ["$eval", "$on"]);
testElement = { offsetWidth: 100, offsetHeight: 200 };
testAttrs = { mctResize: "some-expr" };
@@ -63,6 +63,32 @@ define(
);
});
it("stops size checking for size changes after destroy", function () {
mctResize.link(mockScope, [testElement], testAttrs);
// First, make sure there's a $destroy observer
expect(mockScope.$on)
.toHaveBeenCalledWith("$destroy", jasmine.any(Function));
// Should have scheduled the first timeout
expect(mockTimeout.calls.length).toEqual(1);
// Fire the timeout
mockTimeout.mostRecentCall.args[0]();
// Should have scheduled another timeout
expect(mockTimeout.calls.length).toEqual(2);
// Broadcast a destroy event
mockScope.$on.mostRecentCall.args[1]();
// Fire the timeout
mockTimeout.mostRecentCall.args[0]();
// Should NOT have scheduled another timeout
expect(mockTimeout.calls.length).toEqual(2);
});
});
}
);