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

Commit faf02f0

Browse files
lrlopezmhevery
authored andcommittedJan 19, 2013
feat(route): Allow using functions as template params in 'when'
1 parent b8bd4d5 commit faf02f0

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed
 

‎src/ng/route.js

+28-7
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,24 @@ function $RouteProvider(){
3535
* - `controller` – `{(string|function()=}` – Controller fn that should be associated with newly
3636
* created scope or the name of a {@link angular.Module#controller registered controller}
3737
* if passed as a string.
38-
* - `template` – `{string=}` – html template as a string that should be used by
39-
* {@link ng.directive:ngView ngView} or
38+
* - `template` – `{string=|function()=}` – html template as a string or function that returns
39+
* an html template as a string which should be used by {@link ng.directive:ngView ngView} or
4040
* {@link ng.directive:ngInclude ngInclude} directives.
41-
* this property takes precedence over `templateUrl`.
42-
* - `templateUrl` – `{string=}` – path to an html template that should be used by
43-
* {@link ng.directive:ngView ngView}.
41+
* This property takes precedence over `templateUrl`.
42+
*
43+
* If `template` is a function, it will be called with the following parameters:
44+
*
45+
* - `{Array.<Object>}` - route parameters extracted from the current
46+
* `$location.path()` by applying the current route
47+
*
48+
* - `templateUrl` – `{string=|function()=}` – path or function that returns a path to an html
49+
* template that should be used by {@link ng.directive:ngView ngView}.
50+
*
51+
* If `templateUrl` is a function, it will be called with the following parameters:
52+
*
53+
* - `{Array.<Object>}` - route parameters extracted from the current
54+
* `$location.path()` by applying the current route
55+
*
4456
* - `resolve` - `{Object.<string, function>=}` - An optional map of dependencies which should
4557
* be injected into the controller. If any of these dependencies are promises, they will be
4658
* resolved and converted to a value before the controller is instantiated and the
@@ -395,9 +407,18 @@ function $RouteProvider(){
395407
values.push(isString(value) ? $injector.get(value) : $injector.invoke(value));
396408
});
397409
if (isDefined(template = next.template)) {
410+
if (isFunction(template)) {
411+
template = template(next.params);
412+
}
398413
} else if (isDefined(template = next.templateUrl)) {
399-
template = $http.get(template, {cache: $templateCache}).
400-
then(function(response) { return response.data; });
414+
if (isFunction(template)) {
415+
template = template(next.params);
416+
}
417+
if (isDefined(template)) {
418+
next.loadedTemplateUrl = template;
419+
template = $http.get(template, {cache: $templateCache}).
420+
then(function(response) { return response.data; });
421+
}
401422
}
402423
if (isDefined(template)) {
403424
keys.push('$template');

‎test/ng/routeSpec.js

+47
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,53 @@ describe('$route', function() {
715715
});
716716

717717

718+
it('should allow using a function as a template', function() {
719+
var customTemplateWatcher = jasmine.createSpy('customTemplateWatcher');
720+
721+
function customTemplateFn(routePathParams) {
722+
customTemplateWatcher(routePathParams);
723+
expect(routePathParams).toEqual({id: 'id3'});
724+
return '<h1>' + routePathParams.id + '</h1>';
725+
}
726+
727+
module(function($routeProvider){
728+
$routeProvider.when('/bar/:id/:subid/:subsubid', {templateUrl: 'bar.html'});
729+
$routeProvider.when('/foo/:id', {template: customTemplateFn});
730+
});
731+
732+
inject(function($route, $location, $rootScope) {
733+
$location.path('/foo/id3');
734+
$rootScope.$digest();
735+
736+
expect(customTemplateWatcher).toHaveBeenCalledWith({id: 'id3'});
737+
});
738+
});
739+
740+
741+
it('should allow using a function as a templateUrl', function() {
742+
var customTemplateUrlWatcher = jasmine.createSpy('customTemplateUrlWatcher');
743+
744+
function customTemplateUrlFn(routePathParams) {
745+
customTemplateUrlWatcher(routePathParams);
746+
expect(routePathParams).toEqual({id: 'id3'});
747+
return 'foo.html';
748+
}
749+
750+
module(function($routeProvider){
751+
$routeProvider.when('/bar/:id/:subid/:subsubid', {templateUrl: 'bar.html'});
752+
$routeProvider.when('/foo/:id', {templateUrl: customTemplateUrlFn});
753+
});
754+
755+
inject(function($route, $location, $rootScope) {
756+
$location.path('/foo/id3');
757+
$rootScope.$digest();
758+
759+
expect(customTemplateUrlWatcher).toHaveBeenCalledWith({id: 'id3'});
760+
expect($route.current.loadedTemplateUrl).toEqual('foo.html');
761+
});
762+
});
763+
764+
718765
describe('reload', function() {
719766

720767
it('should reload even if reloadOnSearch is false', function() {

0 commit comments

Comments
 (0)
This repository has been archived.