davej / angular-classy

Cleaner class-based controllers with Angular 1
http://davej.github.io/angular-classy/
813 stars 27 forks source link

Alias "this" to something else so we don't get into binding trouble within callbacks #26

Closed demetriusnunes closed 10 years ago

demetriusnunes commented 10 years ago

I think using "this" to refer to the controller is a bad idea, as often you'll introduce buggy code within function callbacks refering to the wrong "this".

Maybe it would be better to have a built-in & safe alias for "this", such as "self", "_this", "ctrl", "me", "$", "$$", or something else.

davej commented 10 years ago

It's not possible to use another keyword, this is the only object that will persist class-wide in javascript (unless you want to create a global). See #16 for some ways to mitigate issues with the this keyword in callbacks.

Let me know if I'm misunderstanding your issue.

demetriusnunes commented 10 years ago

I thought that maybe it would be possible to create a closed scope around the controller using a function closure and bind the controller object to a class-wide variable.

But I see that you primarily use CoffeeScript so that's probably not a priority right now. :)