angular / router

The Angular 1 Component Router
MIT License
665 stars 135 forks source link

canActivate not called from prototype #304

Closed mafredri closed 9 years ago

mafredri commented 9 years ago

canActivate does not follow the same pattern as activate, deactivate and canDeactivate. Tested on latest master (077bfb6172074662ed4ab5fad6d624790fb4f3e2, built with gulp).

Fails:

AuthController.prototype.canActivate = function canActivate() {
  console.log('Auth canActivate');
};

Works:

AuthController.canActivate = function canActivate() {
  console.log('Auth canActivate');
};
btford commented 9 years ago

This is intended behavior.

You don't know whether or not to instantiate a component until after you figure out if you can navigate to it.

mafredri commented 9 years ago

Ok, I think I understand now, but just to confirm that there is no miscommunication: This behavior is different between v0.5.3 and master, is that how it's supposed to be? Documentation also states it should be as in v0.5.3, not as it is on master.

I can understand the logic for having canActivate as static, but it brings some limitations:

Isn't the whole reason for having a activate lifecycle hook there to perform tasks when the controller in fact gets activated? In my opinion it should always be ok to instantiate a component (assuming this refers to the controller), and then perform the canActivate check.

btford commented 9 years ago

@mafredri – this won't fly for Angular 1. It would mean creating $scopes and registering watches if the constructor asks for them, or it would mean that you cannot inject $scope into controller constructors (which was previously the case).

mafredri commented 9 years ago

That makes sense, thanks for the clarifications! Was just surprised to discover the API change, glad to know there are valid reasons behind it.