I’m testing out crossroads.js and hasher.js together and the following code errors on the second setHash call, with “Function.prototype.apply: argument is not an Object”. This is both in IE and Chrome.
crossroads.addRoute( 'lorem/{id}', function( id ) { alert( id ); } );
hasher.changed.add( crossroads.parse, crossroads );
hasher.init();
hasher.setHash('lorem/123'); // works with alert(123)
hasher.setHash('lorem/456'); // javascript error
Am I doing something wrong here? The first setHash shows the alert fine.
I worked out what the problem is. The
crossroads.parsehas the wrong signature (request, defaultArgs) for thehasher.changedsignal and shouldn’t be added directly with this code:hasher.changed.add(crossroads.parse, crossroads). If I pass in a proxy method with signature (newHash, oldHash) it works:The reason it works the first time is because there’s no
oldHashand so nodefaultArgs. AnddefaultArgsgets initialised to an empty array at the beginning of theparsemethod if it’s blank.But this works a little too well because now I’m now getting two alerts for each
setHashcall.