dotJEM / angular-routing

Enhanced state based routing for Angular applications!
https://dotjem.github.io/angular-routing/
MIT License
75 stars 9 forks source link

Use converters to convert to string #12

Closed jeme closed 11 years ago

jeme commented 11 years ago

When interpolating routes, either through a $state.goto call for a state associated with a route, or directly with $route.change, the converters registered in the route should be used to convert reversely.

This is to enable the developer to write more elaborate converters that convert a simple string into an object, that object should be able to be converted back.

(It is not a recommendation that developers choose to do that, but since they are able to we have to provide the reverse as well)

Example of a converter today:

    this.convert('num', () => {
        return (param) => {
            var accepts = !isNaN(param);
            return {
                accept: accepts,
                value: accepts ? Number(param) : 0
            };
        }
    });

Example of a converter in the future?:

    //Note: that we can't use TypeScript Lambda notation in this case as it would 
    //      point to the wrong object.
    this.convert('num', function () {
        this.toValue = (param) => {
            var accepts = !isNaN(param);
            return {
                accept: accepts,
                value: accepts ? Number(param) : 0
            };
        }
        this.toString = (value) => {...}
    });
jeme commented 11 years ago

Final design: (example taken from numeric converter)

.convert('num', () => {
        return {
            parse: (param) => {
                var accepts = !isNaN(param);
                return {
                    accept: accepts,
                    value: accepts ? Number(param) : 0
                };
            },
            format: (value) => {
                if (isNaN(value))
                    throw new Error("Value was not acceptable for a numeric parameter.");
                return value.toString();
            }
        };
    });