Skip to content

Commit 4059545

Browse files
nateabelechristopherthielen
authored andcommitted
refactor($state): use helper funcs to scope params
Refactor $stateParams to be built by factory, so internal params and watchers can be localized.
1 parent 5c5509f commit 4059545

File tree

1 file changed

+61
-57
lines changed

1 file changed

+61
-57
lines changed

src/state.js

+61-57
Original file line numberDiff line numberDiff line change
@@ -924,9 +924,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
924924
$stateParams.$off();
925925

926926
if (options.location && to.navigable) {
927-
$urlRouter.push(to.navigable.url, to.navigable.locals.globals.$stateParams, {
928-
replace: options.location === 'replace'
929-
});
927+
$urlRouter.push(to.navigable.url, $stateParams, { replace: options.location === 'replace' });
930928
}
931929

932930
if (options.notify) {
@@ -1159,29 +1157,30 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
11591157
return (state && state.self) ? state.self : null;
11601158
};
11611159

1162-
function resolveState(state, params, paramsAreFiltered, inherited, dst) {
1160+
function resolveState(state, params, filtered, inherited, dst) {
11631161
// Make a restricted $stateParams with only the parameters that apply to this state if
11641162
// necessary. In addition to being available to the controller and onEnter/onExit callbacks,
11651163
// we also need $stateParams to be available for any $injector calls we make during the
11661164
// dependency resolution process.
1167-
var $stateParams = (paramsAreFiltered) ? params : filterByKeys(objectKeys(state.params), params);
1168-
var locals = { $stateParams: $stateParams };
1165+
var locals = { $stateParams: (filtered) ? params : $stateParams.$localize(state, params) };
11691166

11701167
// Resolve 'global' dependencies for the state, i.e. those not specific to a view.
11711168
// We're also including $stateParams in this; that way the parameters are restricted
11721169
// to the set that should be visible to the state, and are independent of when we update
11731170
// the global $state and $stateParams values.
11741171
dst.resolve = $resolve.resolve(state.resolve, locals, dst.resolve, state);
1172+
11751173
var promises = [dst.resolve.then(function (globals) {
11761174
dst.globals = globals;
11771175
})];
1176+
11781177
if (inherited) promises.push(inherited);
11791178

11801179
// Resolve template and dependencies for all views.
11811180
forEach(state.views, function (view, name) {
11821181
var injectables = (view.resolve && view.resolve !== state.resolve ? view.resolve : {});
11831182
injectables.$template = [ function () {
1184-
return $view.load(name, { view: view, locals: locals, params: $stateParams }) || '';
1183+
return $view.load(name, { view: view, locals: locals, params: locals.$stateParams }) || '';
11851184
}];
11861185

11871186
promises.push($resolve.resolve(injectables, locals, dst.resolve, state).then(function (result) {
@@ -1212,73 +1211,78 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
12121211
$StateParamsProvider.$inject = [];
12131212
function $StateParamsProvider() {
12141213

1215-
var observers = {}, current = {};
1214+
function stateParamsFactory() {
1215+
var observers = {}, current = {};
12161216

1217-
function unhook(key, func) {
1218-
return function() {
1219-
forEach(key.split(" "), function(k) {
1220-
observers[k].splice(observers[k].indexOf(func), 1);
1217+
function unhook(key, func) {
1218+
return function() {
1219+
forEach(key.split(" "), function(k) {
1220+
observers[k].splice(observers[k].indexOf(func), 1);
1221+
});
1222+
};
1223+
}
1224+
1225+
function observeChange(key, val) {
1226+
if (!observers[key] || !observers[key].length) return;
1227+
1228+
forEach(observers[key], function(func) {
1229+
func(val);
12211230
});
1222-
};
1223-
}
1231+
}
12241232

1225-
function observeChange(key, val) {
1226-
if (!observers[key] || !observers[key].length) return;
1233+
function StateParams() {
1234+
}
12271235

1228-
forEach(observers[key], function(func) {
1229-
func();
1230-
});
1231-
}
1236+
StateParams.prototype.$digest = function() {
1237+
forEach(this, function(val, key) {
1238+
if (val == current[key] || !this.hasOwnProperty(key)) return;
1239+
current[key] = val;
1240+
observeChange(key, val);
1241+
}, this);
1242+
};
12321243

1233-
function StateParams() {
1234-
}
1244+
StateParams.prototype.$set = function(params) {
1245+
forEach(params, function(val, key) {
1246+
this[key] = val;
1247+
observeChange(key);
1248+
}, this);
1249+
this.$sync();
1250+
};
12351251

1236-
StateParams.prototype.$digest = function() {
1237-
forEach(this, function(val, key) {
1238-
if (val == current[key] || !this.hasOwnProperty(key)) return;
1239-
current[key] = val;
1240-
observeChange(key, val);
1241-
}, this);
1242-
};
1252+
StateParams.prototype.$sync = function() {
1253+
copy(this, current);
1254+
};
12431255

1244-
StateParams.prototype.$set = function(params) {
1245-
forEach(params, function(val, key) {
1246-
this[key] = val;
1247-
observeChange(key);
1248-
}, this);
1249-
this.$sync();
1250-
};
1256+
StateParams.prototype.$off = function() {
1257+
observers = {};
1258+
};
12511259

1252-
StateParams.prototype.$sync = function() {
1253-
copy(this, current);
1254-
};
1260+
StateParams.prototype.$localize = function(state, params) {
1261+
var localized = new StateParams();
1262+
params = params || this;
12551263

1256-
StateParams.prototype.$off = function() {
1257-
observers = {};
1258-
};
1264+
forEach(state.params, function(val, key) {
1265+
localized[key] = params[key];
1266+
});
1267+
return localized;
1268+
};
12591269

1260-
StateParams.prototype.$localize = function(state) {
1261-
var localized = new StateParams();
1270+
StateParams.prototype.$observe = function(key, func) {
1271+
forEach(key.split(" "), function(k) {
1272+
(observers[k] || (observers[k] = [])).push(func);
1273+
});
1274+
return unhook(key, func);
1275+
};
12621276

1263-
forEach(state.params, function(val, key) {
1264-
localized[key] = this[key];
1265-
}, this);
1266-
return localized;
1267-
};
1277+
return new StateParams();
1278+
}
12681279

1269-
StateParams.prototype.$observe = function(key, func) {
1270-
forEach(key.split(" "), function(k) {
1271-
(observers[k] || (observers[k] = [])).push(func);
1272-
});
1273-
return unhook(key, func);
1274-
};
1280+
var global = stateParamsFactory();
12751281

12761282
this.$get = $get;
12771283
$get.$inject = ['$rootScope'];
12781284
function $get( $rootScope) {
12791285

1280-
var global = new StateParams();
1281-
12821286
$rootScope.$watch(function() {
12831287
global.$digest();
12841288
});

0 commit comments

Comments
 (0)