Skip to content

Commit a480d1e

Browse files
committed
First pass to extract $view into separate service.
1 parent cdc8755 commit a480d1e

File tree

5 files changed

+61
-18
lines changed

5 files changed

+61
-18
lines changed

Gruntfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module.exports = function (grunt) {
3636
'src/urlMatcherFactory.js',
3737
'src/urlRouter.js',
3838
'src/state.js',
39+
'src/view.js',
3940
'src/viewDirective.js',
4041
'src/stateDirectives.js',
4142
'src/compat.js'

config/karma.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ files = [
1616
'src/templateFactory.js',
1717
'src/urlMatcherFactory.js',
1818
'src/urlRouter.js',
19+
'src/view.js',
1920
'src/state.js',
2021
'src/viewDirective.js',
2122
'src/stateDirectives.js',

src/state.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
186186

187187
// $urlRouter is injected just to ensure it gets instantiated
188188
this.$get = $get;
189-
$get.$inject = ['$rootScope', '$q', '$templateFactory', '$injector', '$stateParams', '$location', '$urlRouter'];
190-
function $get( $rootScope, $q, $templateFactory, $injector, $stateParams, $location, $urlRouter) {
189+
$get.$inject = ['$rootScope', '$q', '$view', '$injector', '$stateParams', '$location', '$urlRouter'];
190+
function $get( $rootScope, $q, $view, $injector, $stateParams, $location, $urlRouter) {
191191

192192
var TransitionSuperseded = $q.reject(new Error('transition superseded'));
193193
var TransitionPrevented = $q.reject(new Error('transition prevented'));
@@ -332,14 +332,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
332332
// necessary. In addition to being available to the controller and onEnter/onExit callbacks,
333333
// we also need $stateParams to be available for any $injector calls we make during the
334334
// dependency resolution process.
335-
var $stateParams;
336-
if (paramsAreFiltered) $stateParams = params;
337-
else {
338-
$stateParams = {};
339-
forEach(state.params, function (name) {
340-
$stateParams[name] = params[name];
341-
});
342-
}
335+
var $stateParams = (paramsAreFiltered) ? params : filterByKeys(state.params, params);
343336
var locals = { $stateParams: $stateParams };
344337

345338
// Resolves the values from an individual 'resolve' dependency spec
@@ -366,21 +359,21 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
366359
// Resolve template and dependencies for all views.
367360
forEach(state.views, function (view, name) {
368361
// References to the controller (only instantiated at link time)
369-
var $view = dst[name] = {
362+
var _$view = dst[name] = {
370363
$$controller: view.controller
371364
};
372365

373366
// Template
374367
promises.push($q
375-
.when($templateFactory.fromConfig(view, $stateParams, locals) || '')
368+
.when($view.load(name, { view: view, locals: locals, notify: false }) || '')
376369
.then(function (result) {
377-
$view.$template = result;
370+
_$view.$template = result;
378371
}));
379372

380373
// View-local dependencies. If we've reused the state definition as the default
381374
// view definition in .state(), we can end up with state.resolve === view.resolve.
382375
// Avoid resolving everything twice in that case.
383-
if (view.resolve !== state.resolve) resolve(view.resolve, $view);
376+
if (view.resolve !== state.resolve) resolve(view.resolve, _$view);
384377
});
385378

386379
// Once we've resolved all the dependencies for this state, merge
@@ -422,6 +415,15 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
422415
}
423416
return true;
424417
}
418+
419+
function filterByKeys(keys, values) {
420+
var filtered = {};
421+
422+
forEach(keys, function (name) {
423+
filtered[name] = values[name];
424+
});
425+
return filtered;
426+
}
425427
}
426428

427429
angular.module('ui.state')

src/view.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
$ViewProvider.$inject = [];
3+
function $ViewProvider() {
4+
5+
this.$get = $get;
6+
$get.$inject = ['$rootScope', '$templateFactory', '$stateParams'];
7+
function $get( $rootScope, $templateFactory, $stateParams) {
8+
return {
9+
// $view.load('full.viewName', { template: ..., controller: ..., resolve: ..., async: false })
10+
load: function load(name, options) {
11+
var result;
12+
options = extend({
13+
template: null, controller: null, view: null, locals: null, notify: true, async: true
14+
}, options);
15+
16+
if (options.view) {
17+
result = $templateFactory.fromConfig(options.view, $stateParams, options.locals);
18+
}
19+
if (result && options.notify) {
20+
$rootScope.$broadcast('$viewContentLoading', options);
21+
}
22+
return result;
23+
}
24+
};
25+
}
26+
}
27+
28+
angular.module('ui.state').provider('$view', $ViewProvider);

src/viewDirective.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function $ViewDirective( $state, $compile, $controller, $injector, $an
44
// TODO: Change to $injector.has() when we version bump to Angular 1.1.5.
55
// See: https://github.com/angular/angular.js/blob/master/CHANGELOG.md#115-triangle-squarification-2013-05-22
66
var $animator; try { $animator = $injector.get('$animator'); } catch (e) { /* do nothing */ }
7-
7+
var viewIsUpdating = false;
88

99
var directive = {
1010
restrict: 'ECA',
@@ -52,7 +52,19 @@ function $ViewDirective( $state, $compile, $controller, $injector, $an
5252
var view = { name: name, state: null };
5353
element.data('$uiView', view);
5454

55-
scope.$on('$stateChangeSuccess', function() { updateView(true); });
55+
var eventHook = function() {
56+
if (viewIsUpdating) return;
57+
viewIsUpdating = true;
58+
59+
try { updateView(true); } catch (e) {
60+
viewIsUpdating = false;
61+
throw e;
62+
}
63+
viewIsUpdating = false;
64+
};
65+
66+
scope.$on('$stateChangeSuccess', eventHook);
67+
scope.$on('$viewContentLoading', eventHook);
5668
updateView(false);
5769

5870
function updateView(doAnimate) {
@@ -80,8 +92,7 @@ function $ViewDirective( $state, $compile, $controller, $injector, $an
8092
viewLocals = locals;
8193
view.state = locals.$$state;
8294

83-
var contents = render.populate(locals.$template, element);
84-
var link = $compile(contents);
95+
var link = $compile(render.populate(locals.$template, element));
8596
viewScope = scope.$new();
8697

8798
if (locals.$$controller) {

0 commit comments

Comments
 (0)