{"action":"closed","issue":{"url":"https://api.github.com/repos/angular-ui/ui-router/issues/2199","repository_url":"https://api.github.com/repos/angular-ui/ui-router","labels_url":"https://api.github.com/repos/angular-ui/ui-router/issues/2199/labels{/name}","comments_url":"https://api.github.com/repos/angular-ui/ui-router/issues/2199/comments","events_url":"https://api.github.com/repos/angular-ui/ui-router/issues/2199/events","html_url":"https://github.com/angular-ui/ui-router/issues/2199","id":102813719,"number":2199,"title":"[$urlMatcherFactoryProvider] Pattern defined in definitionFn is not resolved correctly","user":{"login":"khashayar","id":4737105,"avatar_url":"https://avatars.githubusercontent.com/u/4737105?v=3","gravatar_id":"","url":"https://api.github.com/users/khashayar","html_url":"https://github.com/khashayar","followers_url":"https://api.github.com/users/khashayar/followers","following_url":"https://api.github.com/users/khashayar/following{/other_user}","gists_url":"https://api.github.com/users/khashayar/gists{/gist_id}","starred_url":"https://api.github.com/users/khashayar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/khashayar/subscriptions","organizations_url":"https://api.github.com/users/khashayar/orgs","repos_url":"https://api.github.com/users/khashayar/repos","events_url":"https://api.github.com/users/khashayar/events{/privacy}","received_events_url":"https://api.github.com/users/khashayar/received_events","type":"User","site_admin":false},"labels":[{"id":55736353,"url":"https://api.github.com/repos/angular-ui/ui-router/labels/triage","name":"triage","color":"fef2c0","default":false},{"id":213045728,"url":"https://api.github.com/repos/angular-ui/ui-router/labels/waiting-for-response","name":"waiting-for-response","color":"fef2c0","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":6,"created_at":"2015-08-24T15:03:24Z","updated_at":"2016-12-18T20:25:07Z","closed_at":"2016-12-18T20:25:07Z","body":"I've found an issue with `$urlMatcherFactoryProvider` and custom type definition **function**. Here's a sample config:\n\n``` javascript\napp.config(function ($stateProvider, $urlMatcherFactoryProvider) {\n  $urlMatcherFactoryProvider.type('myType', {}, function () {\n    var typeMap = ['foo', 'bar', 'baz'];\n\n    return {\n      encode: function (val) { return typeMap.indexOf(val); },\n      decode: function (val) { return typeMap[parseInt(val, 10)]; },\n      is:     function (val) { return typeMap.indexOf(val) > -1; },\n      pattern: /\\d/\n    };\n  });\n\n  $stateProvider\n  .state('home', {\n    url: '/',\n    template: 'home.html'\n  })\n  .state('home.documents', {\n    url: '{entityType: myType}{entityId: int}/documents',\n    template: 'documents.html'\n  })\n});\n```\n\nIf I navigate from home to document, let's say `/125/documents`, everything works as expected, $state params are:\n`{entityType: 1, entityId: 25}`\n\nbut once I enter url directly in the browser's address bar, urlMatcher parses the params incorrectly:  `{entityType: 12, entityId: 5}`\n\nAlthough I've specified `pattern: /\\d/` in the type config.\n\nI've looked into the source code and found what causes the issue. Inside `this.type` function, there's this line:\n\n``` javascript\n    $types[name] = new Type(extend({ name: name }, definition));\n    if (definitionFn) {\n      typeQueue.push({ name: name, def: definitionFn });\n      if (!enqueue) flushTypeQueue();\n    }\n```\n\nWhen it comes here `enqueue` is `true` and it doesn't apply the `definitionFn` at this point. Then it goes to `matchDetails` function where it tries to read the type's config:\n\n``` javascript\ntype = $$UMFP.type(regexp || \"string\") || inherit($$UMFP.type(\"string\"), { pattern: new RegExp(regexp, config.caseInsensitive ? 'i' : undefined) });\n```\n\nAnd since the definitionFn is not applied on the $type yet, the pattern is the default pattern `/.*/` and that's where the problem happens.\n\nI know that there is this line inside `this.$get`:\n\n``` javascript\n    enqueue = false;\n    flushTypeQueue();\n```\n\nBut it's too late and `matchDetails` function has already been called and applying the definitionFn has no impact at this point.\n\nSo my question is why there's this line inside `this.type` function:\n\n``` javascript\nif (!enqueue) flushTypeQueue();\n```\n\nand based on the comments in the code\n\n``` javascript\n// `flushTypeQueue()` waits until `$urlMatcherFactory` is injected before invoking the queued `definitionFn`s\n```\n\nWhy `flushTypeQueue` waits for until  `$urlMatcherFactory` is injected?\n"}}
{"actor":{"display_login":"christopherthielen"}}