Skip to content

Commit

Permalink
fix($state): register states at config-time
Browse files Browse the repository at this point in the history
fix($urlMatcherFactory): register types at config-time

- allows Types to be referenced before runtime
- reverts 97f8d90
- partial revert 838b747

fix($state): handle parent.name when parent is obj

 - during state registration, properly grab parent.name and queue state definition, if parent is an object reference
 - Adding comment for commit 838b747
  • Loading branch information
christopherthielen committed Oct 21, 2014
1 parent 554e73c commit 4533fe3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 30 deletions.
7 changes: 2 additions & 5 deletions src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
$StateProvider.$inject = ['$urlRouterProvider', '$urlMatcherFactoryProvider'];
function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {

var root, states = {}, $state, queue = {}, abstractKey = 'abstract', isRuntime = false;
var root, states = {}, $state, queue = {}, abstractKey = 'abstract';

// Builds state properties from definition passed to registerState()
var stateBuilder = {
Expand Down Expand Up @@ -182,7 +182,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
: '';

// If parent is not registered yet, add state to queue and register later
if (name !== "" && (!isRuntime || !states[parentName])) {
if (parentName && !states[parentName]) {
return queueState(parentName, state.self);
}

Expand Down Expand Up @@ -1195,9 +1195,6 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
});
}

isRuntime = true;
flushQueuedChildren("");

return $state;
}

Expand Down
50 changes: 27 additions & 23 deletions src/urlMatcherFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ function $UrlMatcherFactory() {
}
}

var $types, enqueue = true, typeQueue = [], injector, defaultTypes = {
var $types = {}, enqueue = true, typeQueue = [], injector, defaultTypes = {
"searchParam": {
encode: normalizeStringOrArray,
decode: normalizeStringOrArray,
Expand Down Expand Up @@ -610,8 +610,11 @@ function $UrlMatcherFactory() {
* generate URLs with typed parameters.
*
* @param {string} name The type name.
* @param {Object|Function} def The type definition. See
* @param {Object|Function} definition The type definition. See
* {@link ui.router.util.type:Type `Type`} for information on the values accepted.
* @param {Object|Function} definitionFn (optional) A function that is injected before the app
* runtime starts. The result of this function is merged into the existing `definition`.
* See {@link ui.router.util.type:Type `Type`} for information on the values accepted.
*
* @returns {Object} Returns `$urlMatcherFactoryProvider`.
*
Expand Down Expand Up @@ -659,7 +662,7 @@ function $UrlMatcherFactory() {
* // Defines a custom type that gets a value from a service,
* // where each service gets different types of values from
* // a backend API:
* $urlMatcherFactoryProvider.type('dbObject', function(Users, Posts) {
* $urlMatcherFactoryProvider.type('dbObject', {}, function(Users, Posts) {
*
* // Matches up services to URL parameter names
* var services = {
Expand Down Expand Up @@ -704,21 +707,35 @@ function $UrlMatcherFactory() {
* });
* </pre>
*/
this.type = function (name, def) {
if (!isDefined(def)) {
if (!isDefined($types)) throw new Error("Please wait until runtime to retrieve types.");
return $types[name];
this.type = function (name, definition, definitionFn) {
if (!isDefined(definition)) return $types[name];
if ($types.hasOwnProperty(name)) throw new Error("A type named '" + name + "' has already been defined.");

$types[name] = new Type(definition);
if (definitionFn) {
typeQueue.push({ name: name, def: definitionFn });
if (!enqueue) flushTypeQueue();
}
typeQueue.push({ name: name, def: def });
if (!enqueue) flushTypeQueue();
return this;
};

// `flushTypeQueue()` waits until `$urlMatcherFactory` is injected before invoking the queued `definitionFn`s
function flushTypeQueue() {
while(typeQueue.length) {
var type = typeQueue.shift();
if (type.pattern) throw new Error("You cannot override a type's .pattern at runtime.");
angular.extend($types[type.name], injector.invoke(type.def));
}
}

// Register default types. Store them in the prototype of $types.
forEach(defaultTypes, function(type, name) { $types[name] = new Type(type); });
$types = inherit($types, {});

/* No need to document $get, since it returns this */
this.$get = ['$injector', function ($injector) {
injector = $injector;
enqueue = false;
$types = {};
flushTypeQueue();

forEach(defaultTypes, function(type, name) {
Expand All @@ -727,19 +744,6 @@ function $UrlMatcherFactory() {
return this;
}];

// To ensure proper order of operations in object configuration, and to allow internal
// types to be overridden, `flushTypeQueue()` waits until `$urlMatcherFactory` is injected
// before actually wiring up and assigning type definitions
function flushTypeQueue() {
forEach(typeQueue, function(type) {
if ($types[type.name]) {
throw new Error("A type named '" + type.name + "' has already been defined.");
}
var def = new Type(isInjectable(type.def) ? injector.invoke(type.def) : type.def);
$types[type.name] = def;
});
}

this.Param = function Param(id, type, config) {
var self = this;
var defaultValueConfig = getDefaultValueConfig(config);
Expand Down
4 changes: 2 additions & 2 deletions test/urlMatcherFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ describe("urlMatcherFactory", function () {
});

it("should accept injected function definitions", inject(function ($stateParams) {
$umf.type("myType", function($stateParams) {
$umf.type("myType", {}, function($stateParams) {
return {
decode: function() {
return $stateParams;
Expand All @@ -241,7 +241,7 @@ describe("urlMatcherFactory", function () {
}));

it("should accept annotated function definitions", inject(function ($stateParams) {
$umf.type("myAnnotatedType", ['$stateParams', function(s) {
$umf.type("myAnnotatedType", {},['$stateParams', function(s) {
return {
decode: function() {
return s;
Expand Down

0 comments on commit 4533fe3

Please sign in to comment.