millermedeiros / crossroads.js

JavaScript Routes
http://millermedeiros.github.com/crossroads.js/
1.44k stars 156 forks source link

add option to set how parameters should be passed to handlers #31

Closed millermedeiros closed 12 years ago

millermedeiros commented 13 years ago

right now crossroads uses route.matched.dispatch.apply() to pass captured values as parameters, I think we should create an option to toggle between passing parameters as an Array or calling apply()... it will probably increase 2-3 lines of code and can reduce the need of using normalize_ just for that reason...

maybe we should even add a third option to return parameters as an object (like the one we pass to normalize and validation methods..) all the heavy logic is already there, only problem I can foresee is that normalize_ returns an array so it will be impossible to map each value to the real key name (values can be out of order, missing, extra values...), probably user will need to return an object instead of an array if handlers expects objects (which should be totally fine if documented).

not sure if it should be a global setting (per Router) or if each Route should also have it's own setting (that bypass Router setting)...

millermedeiros commented 13 years ago

maybe simply create a property normalizeFn that would be used by all routes and would work like rules.normalize_...

millermedeiros commented 13 years ago

the normalizeFn seems to be flexible enough. needs to document it well otherwise most people won't notice how to use it properly.

millermedeiros commented 12 years ago

considering to add 2 normalize functions to crossroads core, something like:

crossroads.NORM_AS_ARRAY = function (req, vals) {
    //will dispatch a single parameter (Array) with all the values.
    //`vals_` is a special property which contains all captured values.
    return [vals.vals_];
};

crossroads.NORM_AS_OBJECT = function (req, vals) {
    //will dispatch a single parameter (Object) with all the values.
    return [vals];
};

I already have these functions on the docs but since it is so simple I guess it would be better to simply add them to core so the user don't even need to copy and paste it, just need to do:

crossroads.normalizeFn = crossroads.NORM_AS_OBJECT;
millermedeiros commented 12 years ago

maybe keep the methods as constants that way user can reuse for specific routes instead of changing the behavior of the whole router instance..