Skip to content

Commit a5578de

Browse files
feat(uiView): Put $animate promises on element.data('$uiView')
closes #2562 closes #2579
1 parent ba2ba82 commit a5578de

File tree

2 files changed

+53
-12
lines changed

2 files changed

+53
-12
lines changed

src/ng1/viewDirective.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -196,20 +196,22 @@ function $ViewDirective( $view, $animate, $uiViewScroll, $interpolate,
196196

197197
function cleanupLastView() {
198198
if (previousEl) {
199-
trace.traceUiViewEvent("Removing (previous) el", viewData);
199+
trace.traceUiViewEvent("Removing (previous) el", previousEl.data('$uiView'));
200200
previousEl.remove();
201201
previousEl = null;
202202
}
203203

204204
if (currentScope) {
205-
trace.traceUiViewEvent("Destroying (previous) scope", viewData);
205+
trace.traceUiViewEvent("Destroying scope", viewData);
206206
currentScope.$destroy();
207207
currentScope = null;
208208
}
209209

210210
if (currentEl) {
211-
trace.traceUiViewEvent("Animate out (previous)", viewData);
211+
let _viewData = currentEl.data('$uiView');
212+
trace.traceUiViewEvent("Animate out", _viewData);
212213
renderer.leave(currentEl, function() {
214+
_viewData.$$animLeave.resolve();
213215
previousEl = null;
214216
});
215217

@@ -222,17 +224,22 @@ function $ViewDirective( $view, $animate, $uiViewScroll, $interpolate,
222224
config = config || <any> {};
223225
let newScope = scope.$new();
224226
trace.traceUiViewScopeCreated(viewData, newScope);
227+
let animEnter = $q.defer(), animLeave = $q.defer();
225228

226-
extend(viewData, {
229+
let $uiViewData = extend({}, viewData, {
227230
context: config.context,
228231
$template: config.template,
229232
$controller: config.controller,
230233
$controllerAs: config.controllerAs,
231-
$locals: config.locals
234+
$locals: config.locals,
235+
$animEnter: animEnter.promise,
236+
$animLeave: animLeave.promise,
237+
$$animLeave: animLeave
232238
});
233239

234240
let cloned = $transclude(newScope, function(clone) {
235-
renderer.enter(clone.data('$uiView', viewData), $element, function onUiViewEnter() {
241+
renderer.enter(clone.data('$uiView', $uiViewData), $element, function onUiViewEnter() {
242+
animEnter.resolve();
236243
if (currentScope) {
237244
currentScope.$emit('$viewContentAnimationEnded');
238245
}

test/viewDirectiveSpec.js

+40-6
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,14 @@ function animateFlush($animate) {
66
$animate && $animate.flush && $animate.flush(); // 1.4
77
}
88

9-
function animateFlush($animate) {
10-
$animate && $animate.triggerCallbacks && $animate.triggerCallbacks(); // 1.2-1.3
11-
$animate && $animate.flush && $animate.flush(); // 1.4
12-
}
13-
149
describe('uiView', function () {
1510
'use strict';
1611

17-
var scope, $compile, elem;
12+
var scope, $compile, elem, log;
1813

1914
beforeEach(function() {
2015
var depends = ['ui.router'];
16+
log = "";
2117

2218
try {
2319
angular.module('ngAnimate');
@@ -113,6 +109,15 @@ describe('uiView', function () {
113109
controller: function ($scope, $element) {
114110
$scope.elementId = $element.attr('id');
115111
}
112+
},
113+
nState = {
114+
template: 'nState',
115+
controller: function ($scope, $element) {
116+
var data = $element.data('$uiView');
117+
$scope.$on("$destroy", function() { log += 'destroy;'});
118+
data.$animEnter.then(function() { log += "animEnter;"});
119+
data.$animLeave.then(function() { log += "animLeave;"});
120+
}
116121
};
117122

118123
beforeEach(module(function ($stateProvider) {
@@ -130,6 +135,7 @@ describe('uiView', function () {
130135
.state('k', kState)
131136
.state('l', lState)
132137
.state('m', mState)
138+
.state('n', nState)
133139
}));
134140

135141
beforeEach(inject(function ($rootScope, _$compile_) {
@@ -578,6 +584,34 @@ describe('uiView', function () {
578584
// No more animations
579585
expect($animate.queue.length).toBe(0);
580586
}));
587+
588+
it ('should expose animation promises to controllers', inject(function($state, $q, $compile, $animate, $transitions) {
589+
$transitions.onStart({}, function($transition$) { log += 'start:' + $transition$.to().name + ';'; });
590+
$transitions.onFinish({}, function($transition$) { log += 'finish:' + $transition$.to().name + ';'; });
591+
$transitions.onSuccess({}, function($transition$) { log += 'success:' + $transition$.to().name + ';'; });
592+
593+
var content = 'Initial Content';
594+
elem.append($compile('<div><ui-view>' + content + '</ui-view></div>')(scope));
595+
$state.transitionTo('n');
596+
$q.flush();
597+
598+
expect($state.current.name).toBe('n');
599+
expect(log).toBe('start:n;finish:n;success:n;');
600+
601+
animateFlush($animate);
602+
$q.flush();
603+
expect(log).toBe('start:n;finish:n;success:n;animEnter;');
604+
605+
$state.transitionTo('a');
606+
$q.flush();
607+
expect($state.current.name).toBe('a');
608+
expect(log).toBe('start:n;finish:n;success:n;animEnter;start:a;finish:a;destroy;success:a;');
609+
610+
animateFlush($animate);
611+
$q.flush();
612+
expect(log).toBe('start:n;finish:n;success:n;animEnter;start:a;finish:a;destroy;success:a;animLeave;');
613+
}));
614+
581615
});
582616
});
583617

0 commit comments

Comments
 (0)