davej / angular-classy

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

Controller chaining? #13

Closed riannucci closed 10 years ago

riannucci commented 10 years ago

With Plain-Old-Angular controllers, I can do (forgive my coffeescript):

angular.module 'dude', []

.controller 'BobCtrl', ->
  ...

.controller 'OtherCtrl', ->
  ...

But if I try this with classy:

angular.module 'dude', ['classy']

.classy.controller 'BobCtrl', ->  # works!
  ...

.classy.controller 'OtherCtrl', ->  # boom! No 'classy' on undefined.
  ...

I suspect that something ought to be returning the module again somewhere, but I'm not sure where to poke (otherwise I'd offer a pull request :)).

Of course, the workaround is easy:

m = angular.module 'dude', ['classy']

m.classy.controller ...
m.classy.controller ...
riannucci commented 10 years ago

(it's also possible that I'm a bad person for wanting to do this :)).

davej commented 10 years ago

Yup, classy.controller will return itself rather than the module. The reason for this is so you can inline a Classy controller in routes and directives and other places. For example:

app.directive('classyDirective', function() {
  return {
    controller: app.classy.controller({
      inject: ['$scope'],
      init: function() {
        this.$.testing = 'worked';
      }
    })
  };
});
riannucci commented 10 years ago

Ah cool. I'll just do the non-dumb thing then :smiley: