Skip to content

Commit

Permalink
feat($state): Inject templateProvider with resolved values
Browse files Browse the repository at this point in the history
  • Loading branch information
webgago committed May 4, 2015
1 parent da63b27 commit afa20f2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 23 deletions.
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

0 comments on commit afa20f2

Please sign in to comment.