davej / angular-classy

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

Add "events" method to classy controller #15

Closed jonaskuiler closed 10 years ago

jonaskuiler commented 10 years ago

It would be great if you could add a method to your controllers called "events", or something like that, where you can bind your scope events (this.scope.$on('event')) and attach handlers to them.

davej commented 10 years ago

Would you be interested in creating a plugin? See here for details: https://github.com/davej/angular-classy/issues/6

I want the next version of Classy to be modular so that you can add/remove Classy features easily using plugins.

wuxiaoying commented 10 years ago

Might be as simple as this =p

on_module = angular.module 'classy-on', ['classy-core']

on_module.classy.plugin.controller
    name: 'on'

    options:
        enabled: true

    isActive: (klass, deps) ->
        if @options.enabled and angular.isObject(klass.on)
            if !deps.$scope
                throw new Error "You need to inject `$scope` to use the watch object"
                return false

            return true

    postInit: (klass, deps, module) ->
        if !@isActive(klass, deps) then return

        for key, fn of klass.on
            deps.$scope.$on key, angular.bind klass, fn
        return
davej commented 10 years ago

@wuxiaoying Thanks, looks good to me (although I haven't tested it)! If you want to create a repo called 'classy-on' (or 'classy-events') and add it to bower then I will link to it on the Angular Classy site when 0.5 has been released.

davej commented 10 years ago

Closing. Please see: https://github.com/wuxiaoying/classy-on