Skip to content

Commit

Permalink
fix($urlMatcherFactory): early binding of array handler bypasses type…
Browse files Browse the repository at this point in the history
… resolution (causes #1048?)
  • Loading branch information
Florian Grandel authored and christopherthielen committed Nov 21, 2014
1 parent 6d0728b commit ada4bc2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/urlMatcherFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,9 @@ Type.prototype.$asArray = function(mode, isSearch) {
return new ArrayType(this, mode);

function ArrayType(type, mode) {
function bindTo(thisObj, callback) {
function bindTo(type, callbackName) {
return function() {
return callback.apply(thisObj, arguments);
return type[callbackName].apply(type, arguments);
};
}

Expand Down Expand Up @@ -537,10 +537,10 @@ Type.prototype.$asArray = function(mode, isSearch) {
};
}

this.encode = arrayHandler(bindTo(this, type.encode));
this.decode = arrayHandler(bindTo(this, type.decode));
this.is = arrayHandler(bindTo(this, type.is), true);
this.equals = arrayEqualsHandler(bindTo(this, type.equals));
this.encode = arrayHandler(bindTo(type, 'encode'));
this.decode = arrayHandler(bindTo(type, 'decode'));
this.is = arrayHandler(bindTo(type, 'is'), true);
this.equals = arrayEqualsHandler(bindTo(type, 'equals'));
this.pattern = type.pattern;
this.$arrayMode = mode;
}
Expand Down
18 changes: 18 additions & 0 deletions test/urlMatcherFactorySpec.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,24 @@ describe("UrlMatcher", function () {
});
});

describe("urlMatcherFactoryProvider", function () {
describe(".type()", function () {
var m;
beforeEach(module('ui.router.util', function($urlMatcherFactoryProvider) {
$urlMatcherFactoryProvider.type("myType", {}, function() {
return { decode: function() { return 'decoded'; }
};
});
m = new UrlMatcher("/test?{foo:myType}");
}));

it("should handle arrays properly with config-time custom type definitions", inject(function ($stateParams) {
expect(m.exec("/test", {foo: '1'})).toEqual({ foo: 'decoded' });
expect(m.exec("/test", {foo: ['1', '2']})).toEqual({ foo: ['decoded', 'decoded'] });
}));
});
});

describe("urlMatcherFactory", function () {

var $umf;
Expand Down

0 comments on commit ada4bc2

Please sign in to comment.