Skip to content

feat($state): Inject templateProvider with resolved values #1934

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

Merged
merged 1 commit into from
May 15, 2015
Merged
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
52 changes: 30 additions & 22 deletions src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -1390,30 +1390,38 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
})];
if (inherited) promises.push(inherited);

// Resolve template and dependencies for all views.
forEach(state.views, function (view, name) {
var injectables = (view.resolve && view.resolve !== state.resolve ? view.resolve : {});
injectables.$template = [ function () {
return $view.load(name, { view: view, locals: locals, params: $stateParams, notify: options.notify }) || '';
}];

promises.push($resolve.resolve(injectables, locals, dst.resolve, state).then(function (result) {
// References to the controller (only instantiated at link time)
if (isFunction(view.controllerProvider) || isArray(view.controllerProvider)) {
var injectLocals = angular.extend({}, injectables, locals, result);
result.$$controller = $injector.invoke(view.controllerProvider, null, injectLocals);
} else {
result.$$controller = view.controller;
}
// Provide access to the state itself for internal use
result.$$state = state;
result.$$controllerAs = view.controllerAs;
dst[name] = result;
}));
});
function resolveViews() {
var viewsPromises = [];

// Resolve template and dependencies for all views.
forEach(state.views, function (view, name) {
var injectables = (view.resolve && view.resolve !== state.resolve ? view.resolve : {});
injectables.$template = [ function () {
return $view.load(name, { view: view, locals: dst.globals, params: $stateParams, notify: options.notify }) || '';
}];

viewsPromises.push($resolve.resolve(injectables, dst.globals, dst.resolve, state).then(function (result) {
// References to the controller (only instantiated at link time)
if (isFunction(view.controllerProvider) || isArray(view.controllerProvider)) {
var injectLocals = angular.extend({}, injectables, dst.globals);
result.$$controller = $injector.invoke(view.controllerProvider, null, injectLocals);
} else {
result.$$controller = view.controller;
}
// Provide access to the state itself for internal use
result.$$state = state;
result.$$controllerAs = view.controllerAs;
dst[name] = result;
}));
});

return $q.all(viewsPromises).then(function(){
return dst.globals;
});
}

// Wait for all the promises and then return the activation object
return $q.all(promises).then(function (values) {
return $q.all(promises).then(resolveViews).then(function (values) {
return dst;
});
}
Expand Down
19 changes: 18 additions & 1 deletion test/stateSpec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe('state', function () {

var stateProvider, locationProvider, templateParams, ctrlName;
var stateProvider, locationProvider, templateParams, ctrlName, template;

beforeEach(module('ui.router', function($locationProvider) {
locationProvider = $locationProvider;
Expand Down Expand Up @@ -78,6 +78,16 @@ describe('state', function () {
foo: function() { return 'Foo'; }
}
})
.state('dynamicTemplate', {
url: "/dynamicTemplate/:type",
templateProvider: function($stateParams, foo) {
template = $stateParams.type + foo + "Template";
return template;
},
resolve: {
foo: function() { return 'Foo'; }
}
})
.state('home.redirect', {
url: "redir",
onEnter: function($state) {
Expand Down Expand Up @@ -490,6 +500,12 @@ describe('state', function () {
expect(ctrlName).toEqual("AcmeFooController");
}));+

it('uses the templateProvider to get template dynamically', inject(function ($state, $q) {
$state.transitionTo('dynamicTemplate', { type: "Acme" });
$q.flush();
expect(template).toEqual("AcmeFooTemplate");
}));

it('updates the location #fragment, if specified', inject(function ($state, $q, $location) {
// html5mode disabled
locationProvider.html5Mode(false);
Expand Down Expand Up @@ -929,6 +945,7 @@ describe('state', function () {
'badParam',
'badParam2',
'dynamicController',
'dynamicTemplate',
'first',
'home',
'home.item',
Expand Down