Skip to content

Commit 8941fbf

Browse files
committed
Initial refactor.
- Return home early - Extract repeated animator branching logic into internal service
1 parent fdc1156 commit 8941fbf

File tree

1 file changed

+49
-40
lines changed

1 file changed

+49
-40
lines changed

src/viewDirective.js

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11

22
$ViewDirective.$inject = ['$state', '$compile', '$controller', '$injector', '$anchorScroll'];
33
function $ViewDirective( $state, $compile, $controller, $injector, $anchorScroll) {
4-
// Unfortunately there is no neat way to ask $injector if a service exists
4+
// TODO: Change to $injector.has() when we version bump to Angular 1.1.5.
5+
// See: https://github.com/angular/angular.js/blob/master/CHANGELOG.md#115-triangle-squarification-2013-05-22
56
var $animator; try { $animator = $injector.get('$animator'); } catch (e) { /* do nothing */ }
67

8+
79
var directive = {
810
restrict: 'ECA',
911
terminal: true,
@@ -15,6 +17,28 @@ function $ViewDirective( $state, $compile, $controller, $injector, $an
1517
onloadExp = attr.onload || '',
1618
animate = isDefined($animator) && $animator(scope, attr);
1719

20+
var renderer = function(doAnimate) {
21+
return ({
22+
"true": {
23+
remove: function(element) { animate.leave(element.contents(), element); },
24+
restore: function(compiled, element) { animate.enter(compiled, element); },
25+
populate: function(template, element) {
26+
var contents = angular.element('<div></div>').html(template).contents();
27+
animate.enter(contents, element);
28+
return contents;
29+
}
30+
},
31+
"false": {
32+
remove: function(element) { element.html(''); },
33+
restore: function(compiled, element) { element.append(compiled); },
34+
populate: function(template, element) {
35+
element.html(template);
36+
return element.contents();
37+
}
38+
}
39+
})[doAnimate.toString()];
40+
};
41+
1842
// Put back the compiled initial view
1943
element.append(transclude(scope));
2044

@@ -32,59 +56,44 @@ function $ViewDirective( $state, $compile, $controller, $injector, $an
3256
function updateView(doAnimate) {
3357
var locals = $state.$current && $state.$current.locals[name];
3458
if (locals === viewLocals) return; // nothing to do
59+
var render = renderer(animate && doAnimate);
3560

3661
// Remove existing content
37-
if (animate && doAnimate) {
38-
animate.leave(element.contents(), element);
39-
} else {
40-
element.html('');
41-
}
62+
render.remove(element);
4263

4364
// Destroy previous view scope
4465
if (viewScope) {
4566
viewScope.$destroy();
4667
viewScope = null;
4768
}
4869

49-
if (locals) {
50-
viewLocals = locals;
51-
view.state = locals.$$state;
52-
53-
var contents;
54-
if (animate && doAnimate) {
55-
contents = angular.element('<div></div>').html(locals.$template).contents();
56-
animate.enter(contents, element);
57-
} else {
58-
element.html(locals.$template);
59-
contents = element.contents();
60-
}
61-
62-
var link = $compile(contents);
63-
viewScope = scope.$new();
64-
if (locals.$$controller) {
65-
locals.$scope = viewScope;
66-
var controller = $controller(locals.$$controller, locals);
67-
element.children().data('$ngControllerController', controller);
68-
}
69-
link(viewScope);
70-
viewScope.$emit('$viewContentLoaded');
71-
viewScope.$eval(onloadExp);
72-
73-
// TODO: This seems strange, shouldn't $anchorScroll listen for $viewContentLoaded if necessary?
74-
// $anchorScroll might listen on event...
75-
$anchorScroll();
76-
} else {
70+
if (!locals) {
7771
viewLocals = null;
7872
view.state = null;
7973

8074
// Restore the initial view
81-
var compiledElem = transclude(scope);
82-
if (animate && doAnimate) {
83-
animate.enter(compiledElem, element);
84-
} else {
85-
element.append(compiledElem);
86-
}
75+
return render.restore(transclude(scope), element);
76+
}
77+
78+
viewLocals = locals;
79+
view.state = locals.$$state;
80+
81+
var contents = render.populate(locals.$template, element);
82+
var link = $compile(contents);
83+
viewScope = scope.$new();
84+
85+
if (locals.$$controller) {
86+
locals.$scope = viewScope;
87+
var controller = $controller(locals.$$controller, locals);
88+
element.children().data('$ngControllerController', controller);
8789
}
90+
link(viewScope);
91+
viewScope.$emit('$viewContentLoaded');
92+
if (onloadExp) viewScope.$eval(onloadExp);
93+
94+
// TODO: This seems strange, shouldn't $anchorScroll listen for $viewContentLoaded if necessary?
95+
// $anchorScroll might listen on event...
96+
$anchorScroll();
8897
}
8998
};
9099
}

0 commit comments

Comments
 (0)