Skip to content

Don't render a view for empty content #557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 51 additions & 40 deletions src/viewDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ function $ViewDirective($state, $compile, $controller, $anchorScroll, $injector)
// Returns a set of DOM manipulation functions based on whether animation
// should be performed
var renderer = function (doAnimate) {
return ({
"true": {
return $animate && doAnimate ? {
leave: function (element) { $animate.leave(element); },
enter: function (element, anchor) { $animate.enter(element, null, anchor); }
},
"false": {
} : {
leave: function (element) { element.remove(); },
enter: function (element, anchor) { anchor.after(element); }
}
})[($animate && doAnimate).toString()];
};
};

var directive = {
Expand Down Expand Up @@ -67,7 +64,8 @@ function $ViewDirective($state, $compile, $controller, $anchorScroll, $injector)

function updateView(doAnimate) {
var locals = $state.$current && $state.$current.locals[name],
render = renderer(doAnimate);
render = renderer(doAnimate),
template = null;

if (isDefault) {
isDefault = false;
Expand All @@ -76,46 +74,59 @@ function $ViewDirective($state, $compile, $controller, $anchorScroll, $injector)

if (!locals) {
cleanupLastView();
currentElement = element.clone();
currentElement.html(defaultContent);
render.enter(currentElement, anchor);

currentScope = $scope.$new();
$compile(currentElement.contents())(currentScope);

if (defaultContent.length) {
currentElement = element.clone();
currentElement.html(defaultContent);
render.enter(currentElement, anchor);

currentScope = $scope.$new();
$compile(currentElement.contents())(currentScope);
}

return;
}

if (locals === viewLocals) return; // nothing to do

cleanupLastView();

currentElement = element.clone();
currentElement.html(locals.$template ? locals.$template : defaultContent);
render.enter(currentElement, anchor);

currentElement.data('$uiView', view);

viewLocals = locals;
view.state = locals.$$state;

var link = $compile(currentElement.contents());

currentScope = $scope.$new();

if (locals.$$controller) {
locals.$scope = currentScope;
var controller = $controller(locals.$$controller, locals);
currentElement.children().data('$ngControllerController', controller);

if (locals.$template.length) {
template = locals.$template;
}
else if (defaultContent.length) {
template = defaultContent;
}

if (template !== null) {
currentElement = element.clone();
currentElement.html(template);
render.enter(currentElement, anchor);

currentElement.data('$uiView', view);

viewLocals = locals;
view.state = locals.$$state;

var link = $compile(currentElement.contents());

currentScope = $scope.$new();

if (locals.$$controller) {
locals.$scope = currentScope;
var controller = $controller(locals.$$controller, locals);
currentElement.children().data('$ngControllerController', controller);
}

link(currentScope);

currentScope.$emit('$viewContentLoaded');
if (onloadExp) currentScope.$eval(onloadExp);

// TODO: This seems strange, shouldn't $anchorScroll listen for $viewContentLoaded if necessary?
// $anchorScroll might listen on event...
$anchorScroll();
}

link(currentScope);

currentScope.$emit('$viewContentLoaded');
if (onloadExp) currentScope.$eval(onloadExp);

// TODO: This seems strange, shouldn't $anchorScroll listen for $viewContentLoaded if necessary?
// $anchorScroll might listen on event...
$anchorScroll();
}
};
}
Expand Down
11 changes: 1 addition & 10 deletions test/viewDirectiveSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ describe('uiView', function () {
$state.transitionTo(aState);
$q.flush();

expect($animate.flushNext('leave').element.text()).toBe('');
expect($animate.flushNext('enter').element.text()).toBe(aState.template);
}));

Expand All @@ -100,7 +99,6 @@ describe('uiView', function () {
$state.transitionTo(cState);
$q.flush();

expect($animate.flushNext('leave').element.text()).toBe('');
expect($animate.flushNext('enter').element.text()).toBe(cState.views.cview.template);
}));

Expand All @@ -110,7 +108,6 @@ describe('uiView', function () {
$state.transitionTo(aState);
$q.flush();

expect($animate.flushNext('leave').element.text()).toBe('');
expect($animate.flushNext('enter').element.text()).toBe(aState.template);

$state.transitionTo(bState);
Expand All @@ -126,9 +123,7 @@ describe('uiView', function () {
$state.transitionTo(dState);
$q.flush();

expect($animate.flushNext('leave').element.html()).toBe('');
expect($animate.flushNext('enter').element.text()).toBe(dState.views.dview1.template);
expect($animate.flushNext('leave').element.html()).toBe('');
expect($animate.flushNext('enter').element.text()).toBe(dState.views.dview2.template);
}));

Expand All @@ -138,7 +133,6 @@ describe('uiView', function () {
$state.transitionTo(fState);
$q.flush();

expect($animate.flushNext('leave').element.text()).toBe('');
expect(innerText($animate.flushNext('enter').element.parent()[0].querySelector('.view').querySelector('.eview'))).toBe(fState.views.eview.template);
}));
});
Expand All @@ -153,8 +147,6 @@ describe('uiView', function () {
$state.transitionTo(gState);
$q.flush();

// Leave elem
expect($animate.flushNext('leave').element.text()).toBe("");
// Enter and leave ui-view insert of template
$animate.flushNext('enter');
$animate.flushNext('leave');
Expand All @@ -174,8 +166,7 @@ describe('uiView', function () {
$state.transitionTo(hState);
$q.flush();

expect($animate.queue.length).toEqual(4);
expect($animate.flushNext('leave').element.text()).toBe('');
expect($animate.queue.length).toEqual(3);
expect($animate.flushNext('enter').element.text()).toBe(hState.views.inner.template);

// Remove the addClass observers which have been replaced.
Expand Down