Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controller is instantiated multiple times when using date parameter #2484

Closed
bogdanalexe90 opened this issue Jan 25, 2016 · 6 comments
Closed

Comments

@bogdanalexe90
Copy link

Controller is instantiated multiple times when navigating to a state that uses a date parameter. If date parameter is optional controller is instantiated 3 times else twice.

This is my state definition:

.state('stateName', {
                url: '/{id:int}/{childId:int}?{date:date}',
                params: {
                    childId: null,
                    date: null
                },
                resolve: {
                    ...
                },
                controller: '...',
                templateUrl: '...'
            });

P.s: I'm using 0.2.17 version

@eddiemonge
Copy link
Contributor

Can you provide a plnkr example please?

@bogdanalexe90
Copy link
Author

Here is a plnkr with angular 1.4.8 and ui-router 0.2.16. You can have a look in console for the output.

http://plnkr.co/edit/fmPZXiauQ3TLDGYV5iji?p=preview

@christopherthielen
Copy link
Contributor

Thanks. Verified this is a bug. We should only compare year month and day in .equals method.

@bogdanalexe90
Copy link
Author

@christopherthielen I don't think that's the reason. I re-implemented the date type using angular.equals and it's working properly.

var date = {
            encode: function (date) {
                if (!this.is(date)) {
                    return;
                }

                return [date.getFullYear(), ('0' + (date.getMonth() + 1)).slice(-2), ('0' + date.getDate()).slice(-2)]
                .join('-');
            },
            decode: function (val) {
                var match;

                if (this.is(val)) {
                    return val;
                }

                match = this.capture.exec(val);

                if (!match) {
                    return;
                }

                return new Date(match[1], match[2] - 1, match[3]);
            },
            is: angular.isDate,
            equals: angular.equals,
            pattern: /[0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[1-2][0-9]|3[0-1])/,
            capture: /([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])/
        };

@christopherthielen
Copy link
Contributor

I added your date impl to the plunk and still see double transitions.

http://plnkr.co/edit/k1cPt3vVAq4jQ9zE2ab8?p=preview

@christopherthielen
Copy link
Contributor

This code fixes your plunker:

  app.config(function($stateProvider, $urlMatcherFactoryProvider) {
    let date = $urlMatcherFactoryProvider.type('date')

    date.equals = (l, r) => ['getFullYear', 'getMonth', 'getDate']
        .reduce((memo, fnname) => memo && l[fnname]() === r[fnname](), true);
  });

That equals method belongs here:
https://github.com/angular-ui/ui-router/blob/feature-1.0/src/params/paramTypes.ts#L60

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants