Skip to content

Commit 5160736

Browse files
committed
refactor(StateParams): misc. cleanup
1 parent 75125a4 commit 5160736

File tree

2 files changed

+34
-43
lines changed

2 files changed

+34
-43
lines changed

src/params/stateParams.ts

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
/** @module params */ /** for typedoc */
22
import {IServiceProviderFactory} from "angular";
3-
import {forEach, ancestors, extend, copy} from "../common/common";
3+
import {forEach, ancestors, extend, copy, pick, omit} from "../common/common";
44

5-
export function StateParams() { }
5+
export class StateParams {
6+
constructor(params: Object = {}) {
7+
extend(this, params);
8+
}
9+
10+
$digest() {}
11+
$inherit(newParams, $current, $to) {}
12+
$set(params, url) {}
13+
$sync() {}
14+
$off() {}
15+
$raw() {}
16+
$localize(state, params) {}
17+
$observe(key: string, fn: Function) {}
18+
}
619

720
$StateParamsProvider.$inject = [];
821
function $StateParamsProvider() {
@@ -11,29 +24,23 @@ function $StateParamsProvider() {
1124
let observers = {}, current = {};
1225

1326
function unhook(key, func) {
14-
return function() {
15-
forEach(key.split(" "), function(k) {
16-
observers[k].splice(observers[k].indexOf(func), 1);
17-
});
27+
return () => {
28+
forEach(key.split(" "), k => observers[k].splice(observers[k].indexOf(func), 1));
1829
};
1930
}
2031

2132
function observeChange(key, val?: any) {
2233
if (!observers[key] || !observers[key].length) return;
23-
24-
forEach(observers[key], function(func) {
25-
func(val);
26-
});
34+
forEach(observers[key], func => func(val));
2735
}
2836

2937

3038
StateParams.prototype.$digest = function() {
31-
function updateValue(val, key) {
39+
forEach(this, (val, key) => {
3240
if (val === current[key] || !this.hasOwnProperty(key)) return;
3341
current[key] = val;
3442
observeChange(key, val);
35-
}
36-
forEach(this, updateValue, this);
43+
});
3744
};
3845

3946
/**
@@ -71,14 +78,13 @@ function $StateParamsProvider() {
7178
}
7279
if (abort) return false;
7380

74-
function updateValue(val, key) {
81+
forEach(params, (val, key) => {
7582
if (val !== this[key]) {
7683
this[key] = val;
7784
observeChange(key);
7885
hasChanged = true;
7986
}
80-
}
81-
forEach(params, updateValue, this);
87+
});
8288

8389
this.$sync();
8490
return hasChanged;
@@ -95,29 +101,19 @@ function $StateParamsProvider() {
95101
};
96102

97103
StateParams.prototype.$raw = function() {
98-
let raw = {};
99-
for (let key in this) {
100-
if (!StateParams.prototype.hasOwnProperty(key))
101-
raw[key] = this[key];
102-
}
103-
return raw;
104+
return omit(
105+
this,
106+
Object.keys(this).filter(StateParams.prototype.hasOwnProperty.bind(StateParams.prototype))
107+
);
104108
};
105109

106110
StateParams.prototype.$localize = function(state, params) {
107-
let localized = new StateParams();
108-
params = params || this;
109-
110-
forEach(state.params, function(val, key) {
111-
localized[key] = params[key];
112-
});
113-
return localized;
111+
return new StateParams(pick(params || this, Object.keys(state.params)));
114112
};
115113

116-
StateParams.prototype.$observe = function(key, func) {
117-
forEach(key.split(" "), function(k) {
118-
(observers[k] || (observers[k] = [])).push(func);
119-
});
120-
return unhook(key, func);
114+
StateParams.prototype.$observe = function(key: string, fn: Function) {
115+
forEach(key.split(" "), k => (observers[k] || (observers[k] = [])).push(fn));
116+
return unhook(key, fn);
121117
};
122118

123119
return new StateParams();

src/resolve/resolvable.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** @module path */ /** for typedoc */
22
/// <reference path='../../typings/angularjs/angular.d.ts' />
3-
import {pick, map, filter, not, isFunction} from "../common/common";
4-
import {trace, runtime, } from "../common/module";
3+
import {extend, pick, map, filter, not, isFunction} from "../common/common";
4+
import {trace, runtime} from "../common/module";
55
import {IPromise} from "angular";
66
import {Resolvables, IOptions1} from "./interface"
77

@@ -22,10 +22,7 @@ import {ResolveContext} from "./resolveContext";
2222
*/
2323
export class Resolvable {
2424
constructor(name: string, resolveFn: Function, preResolvedData?: any) {
25-
this.name = name;
26-
this.resolveFn = resolveFn;
27-
this.deps = runtime.$injector.annotate(resolveFn);
28-
this.data = preResolvedData;
25+
extend(this, { name, resolveFn, deps: runtime.$injector.annotate(resolveFn), data: preResolvedData });
2926
}
3027

3128
name: string;
@@ -61,9 +58,7 @@ export class Resolvable {
6158
var depResolvables: Resolvables = <any> pick(ancestorsByName, deps);
6259

6360
// Get promises (or synchronously invoke resolveFn) for deps
64-
var depPromises: any = map(depResolvables, function(resolvable: Resolvable) {
65-
return resolvable.get(resolveContext, options);
66-
});
61+
var depPromises: any = map(depResolvables, (resolvable: Resolvable) => resolvable.get(resolveContext, options));
6762

6863
// Return a promise chain that waits for all the deps to resolve, then invokes the resolveFn passing in the
6964
// dependencies as locals, then unwraps the resulting promise's data.

0 commit comments

Comments
 (0)