Skip to content

Commit ada4bc2

Browse files
Florian Grandelchristopherthielen
Florian Grandel
authored andcommitted
fix($urlMatcherFactory): early binding of array handler bypasses type resolution (causes #1048?)
1 parent 6d0728b commit ada4bc2

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/urlMatcherFactory.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -496,9 +496,9 @@ Type.prototype.$asArray = function(mode, isSearch) {
496496
return new ArrayType(this, mode);
497497

498498
function ArrayType(type, mode) {
499-
function bindTo(thisObj, callback) {
499+
function bindTo(type, callbackName) {
500500
return function() {
501-
return callback.apply(thisObj, arguments);
501+
return type[callbackName].apply(type, arguments);
502502
};
503503
}
504504

@@ -537,10 +537,10 @@ Type.prototype.$asArray = function(mode, isSearch) {
537537
};
538538
}
539539

540-
this.encode = arrayHandler(bindTo(this, type.encode));
541-
this.decode = arrayHandler(bindTo(this, type.decode));
542-
this.is = arrayHandler(bindTo(this, type.is), true);
543-
this.equals = arrayEqualsHandler(bindTo(this, type.equals));
540+
this.encode = arrayHandler(bindTo(type, 'encode'));
541+
this.decode = arrayHandler(bindTo(type, 'decode'));
542+
this.is = arrayHandler(bindTo(type, 'is'), true);
543+
this.equals = arrayEqualsHandler(bindTo(type, 'equals'));
544544
this.pattern = type.pattern;
545545
this.$arrayMode = mode;
546546
}

test/urlMatcherFactorySpec.js

100644100755
+18
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,24 @@ describe("UrlMatcher", function () {
402402
});
403403
});
404404

405+
describe("urlMatcherFactoryProvider", function () {
406+
describe(".type()", function () {
407+
var m;
408+
beforeEach(module('ui.router.util', function($urlMatcherFactoryProvider) {
409+
$urlMatcherFactoryProvider.type("myType", {}, function() {
410+
return { decode: function() { return 'decoded'; }
411+
};
412+
});
413+
m = new UrlMatcher("/test?{foo:myType}");
414+
}));
415+
416+
it("should handle arrays properly with config-time custom type definitions", inject(function ($stateParams) {
417+
expect(m.exec("/test", {foo: '1'})).toEqual({ foo: 'decoded' });
418+
expect(m.exec("/test", {foo: ['1', '2']})).toEqual({ foo: ['decoded', 'decoded'] });
419+
}));
420+
});
421+
});
422+
405423
describe("urlMatcherFactory", function () {
406424

407425
var $umf;

0 commit comments

Comments
 (0)