Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix(ngView): accidentally compiling leaving content
Browse files Browse the repository at this point in the history
closes: #2304
  • Loading branch information
mhevery committed May 2, 2013
1 parent 1d8e11d commit 9956bae
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
15 changes: 9 additions & 6 deletions src/ng/animator.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,17 @@ var $AnimatorProvider = function() {

var durationKey = 'Duration';
var duration = 0;

//we want all the styles defined before and after
forEach(element, function(element) {
var globalStyles = $window.getComputedStyle(element) || {};
duration = Math.max(
parseFloat(globalStyles[w3cTransitionProp + durationKey]) ||
parseFloat(globalStyles[vendorTransitionProp + durationKey]) ||
0,
duration);
if (element.nodeType == 1) {
var globalStyles = $window.getComputedStyle(element) || {};
duration = Math.max(
parseFloat(globalStyles[w3cTransitionProp + durationKey]) ||
parseFloat(globalStyles[vendorTransitionProp + durationKey]) ||
0,
duration);
}
});
$window.setTimeout(done, duration * 1000);
} else {
Expand Down
5 changes: 3 additions & 2 deletions src/ng/directive/ngView.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,10 @@ var ngViewDirective = ['$http', '$templateCache', '$route', '$anchorScroll', '$c

if (template) {
clearContent();
animate.enter(jqLite('<div></div>').html(template).contents(), element);
var enterElements = jqLite('<div></div>').html(template).contents();
animate.enter(enterElements, element);

var link = $compile(element.contents()),
var link = $compile(enterElements),
current = $route.current,
controller;

Expand Down
46 changes: 44 additions & 2 deletions test/ng/directive/ngViewSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
describe('ngView', function() {
var element;

beforeEach(module(function() {
beforeEach(module(function($provide) {
$provide.value('$window', angular.mock.createMockWindow());
return function($rootScope, $compile, $animator) {
element = $compile('<ng:view onload="load()"></ng:view>')($rootScope);
$animator.enabled(true);
Expand Down Expand Up @@ -621,5 +622,46 @@ describe('ngView', function() {
}
}));


it('should not double compile when route changes', function() {
module(function($routeProvider, $animationProvider, $provide) {
$routeProvider.when('/foo', {template: '<div ng-repeat="i in [1,2]">{{i}}</div>'});
$routeProvider.when('/bar', {template: '<div ng-repeat="i in [3,4]">{{i}}</div>'});
$animationProvider.register('my-animation-leave', function() {
return {
start: function(element, done) {
done();
}
};
});
});

inject(function($rootScope, $compile, $location, $route, $window, $rootElement, $sniffer) {
element = $compile(html('<ng:view onload="load()" ng-animate="\'my-animation\'"></ng:view>'))($rootScope);

$location.path('/foo');
$rootScope.$digest();
if ($sniffer.supportsTransitions) {
$window.setTimeout.expect(1).process();
$window.setTimeout.expect(0).process();
}
expect(element.text()).toEqual('12');

$location.path('/bar');
$rootScope.$digest();
expect(n(element.text())).toEqual('1234');
if ($sniffer.supportsTransitions) {
$window.setTimeout.expect(1).process();
$window.setTimeout.expect(1).process();
} else {
$window.setTimeout.expect(1).process();
}
expect(element.text()).toEqual('34');

function n(text) {
return text.replace(/\r\n/m, '').replace(/\r\n/m, '');
}
});
});
});
});
});

0 comments on commit 9956bae

Please sign in to comment.