Skip to content

Commit

Permalink
fix($urlMatcherFactory): add 'any' Type for non-encoding non-url params
Browse files Browse the repository at this point in the history
- Allow arbitrary data be passed to a state as a parameter. Do not attempt to encode/decode the parameter.
- Store the location of the parameter: either "search", "path", or "config" (non-url params are "config")

Closes #1562
  • Loading branch information
christopherthielen committed Nov 21, 2014
1 parent 027f1fc commit 3bfd75a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
ownParams: function(state) {
var params = state.url && state.url.params || new $$UMFP.ParamSet();
forEach(state.params || {}, function(config, id) {
if (!params[id]) params[id] = new $$UMFP.Param(id, null, config);
if (!params[id]) params[id] = new $$UMFP.Param(id, null, config, "config");
});
return params;
},
Expand Down
20 changes: 14 additions & 6 deletions src/urlMatcherFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,13 @@ function $UrlMatcherFactory() {
is: angular.isObject,
equals: angular.equals,
pattern: /[^/]*/
},
any: { // does not encode/decode
encode: angular.identity,
decode: angular.identity,
is: angular.identity,
equals: angular.equals,
pattern: /.*/
}
};

Expand Down Expand Up @@ -878,7 +885,7 @@ function $UrlMatcherFactory() {
this.Param = function Param(id, type, config, location) {
var self = this;
config = unwrapShorthand(config);
type = getType(config, type);
type = getType(config, type, location);
var arrayMode = getArrayMode();
type = arrayMode ? type.$asArray(arrayMode, location === "search") : type;
if (type.name === "string" && !arrayMode && location === "path" && config.value === undefined)
Expand All @@ -896,10 +903,10 @@ function $UrlMatcherFactory() {
return config;
}

function getType(config, urlType) {
function getType(config, urlType, location) {
if (config.type && urlType) throw new Error("Param '"+id+"' has two type configurations.");
if (urlType) return urlType;
if (!config.type) return $types.string;
if (!config.type) return (location === "config" ? $types.any : $types.string);
return config.type instanceof Type ? config.type : new Type(config.type);
}

Expand Down Expand Up @@ -960,13 +967,14 @@ function $UrlMatcherFactory() {
extend(this, {
id: id,
type: type,
location: location,
array: arrayMode,
config: config,
squash: squash,
replace: replace,
isOptional: isOptional,
dynamic: undefined,
value: $value,
dynamic: undefined,
config: config,
toString: toString
});
};
Expand Down Expand Up @@ -1013,7 +1021,7 @@ function $UrlMatcherFactory() {
param = self[key];
val = paramValues[key];
isOptional = !val && param.isOptional;
result = result && (isOptional || param.type.is(val));
result = result && (isOptional || !!param.type.is(val));
});
return result;
},
Expand Down

0 comments on commit 3bfd75a

Please sign in to comment.