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

Route functions (no templates) #1901

Closed
wants to merge 2 commits 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
6 changes: 5 additions & 1 deletion src/ng/directive/ngView.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ var ngViewDirective = ['$http', '$templateCache', '$route', '$anchorScroll', '$c
// $anchorScroll might listen on event...
$anchorScroll();
} else {
clearContent();
if ($route.current && $route.current['$route'] && $route.current['$route'].functionRoute) {
// don't clear content when the route is just a function
} else {
clearContent();
}
}
}
}
Expand Down
76 changes: 46 additions & 30 deletions src/ng/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ function $RouteProvider(){
*
* Object properties:
*
* - `fn` - `{function()=}` - A function to be called when the route is matched. This cannot be used
* in combination with other properties.
* - `controller` – `{(string|function()=}` – Controller fn that should be associated with newly
* created scope or the name of a {@link angular.Module#controller registered controller}
* if passed as a string.
Expand Down Expand Up @@ -398,39 +400,44 @@ function $RouteProvider(){
$q.when(next).
then(function() {
if (next) {
var keys = [],
values = [],
template;

forEach(next.resolve || {}, function(value, key) {
keys.push(key);
values.push(isString(value) ? $injector.get(value) : $injector.invoke(value));
});
if (isDefined(template = next.template)) {
if (isFunction(template)) {
template = template(next.params);
}
} else if (isDefined(template = next.templateUrl)) {
if (isFunction(template)) {
template = template(next.params);
if (next.$route && next.$route.fn && isFunction(next.$route.fn)) {
next.$route.functionRoute = true;
next.$route.fn(next.params);
} else {
var keys = [],
values = [],
template;

forEach(next.resolve || {}, function(value, key) {
keys.push(key);
values.push(isString(value) ? $injector.get(value) : $injector.invoke(value));
});
if (isDefined(template = next.template)) {
if (isFunction(template)) {
template = template(next.params);
}
} else if (isDefined(template = next.templateUrl)) {
if (isFunction(template)) {
template = template(next.params);
}
if (isDefined(template)) {
next.loadedTemplateUrl = template;
template = $http.get(template, {cache: $templateCache}).
then(function(response) { return response.data; });
}
}
if (isDefined(template)) {
next.loadedTemplateUrl = template;
template = $http.get(template, {cache: $templateCache}).
then(function(response) { return response.data; });
keys.push('$template');
values.push(template);
}
}
if (isDefined(template)) {
keys.push('$template');
values.push(template);
}
return $q.all(values).then(function(values) {
var locals = {};
forEach(values, function(value, index) {
locals[keys[index]] = value;
return $q.all(values).then(function(values) {
var locals = {};
forEach(values, function(value, index) {
locals[keys[index]] = value;
});
return locals;
});
return locals;
});
}
}
}).
// after route change
Expand Down Expand Up @@ -465,8 +472,17 @@ function $RouteProvider(){
match.$route = route;
}
});

// No route matched; fallback to "otherwise" route
return match || routes[null] && inherit(routes[null], {params: {}, pathParams:{}});
if (match == null) {
var route = routes[null];
match = route && inherit(route, {params: {}, pathParams:{}});
if (match) {
match.$route = route;
}
}

return match;
}

/**
Expand Down
19 changes: 19 additions & 0 deletions test/ng/directive/ngViewSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,4 +459,23 @@ describe('ngView', function() {
expect(div.controller()).toBe($route.current.scope.ctrl);
});
});

it('should not remove the previous routes content if the current route is a function', function() {
module(function($routeProvider) {
$routeProvider.when('/foo', {template: 'angular is da best'});
$routeProvider.when('/bar', {fn: function() {}});
});

inject(function($rootScope, $compile, $location, $route) {
expect(element.text()).toEqual('');

$location.path('/foo');
$rootScope.$digest();
expect(element.text()).toEqual('angular is da best');

$location.path('/bar');
$rootScope.$digest();
expect(element.text()).toEqual('angular is da best');
});
});
});
23 changes: 23 additions & 0 deletions test/ng/routeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -793,4 +793,27 @@ describe('$route', function() {
});
});
});


describe('route functions', function() {
var routeFunctionSpy = jasmine.createSpy('route function spy');

function routeFunction(routePathParams) {
routeFunctionSpy(routePathParams);
}

beforeEach(module(function($routeProvider) {
$routeProvider.when('/foo/:id', {templateUrl: 'foo.html'});
$routeProvider.when('/bar/:id/:subId', {fn: routeFunction});
}));

it ('should allow using a function as a route', function() {
inject(function($route, $location, $rootScope) {
$location.path('/bar/id3/id4');
$rootScope.$digest();

expect(routeFunctionSpy).toHaveBeenCalledWith({id: 'id3', subId: 'id4'});
});
});
});
});