Skip to content

Instantly share code, notes, and snippets.

@vyorkin
Created February 24, 2014 04:48
Show Gist options
  • Save vyorkin/9182106 to your computer and use it in GitHub Desktop.
Save vyorkin/9182106 to your computer and use it in GitHub Desktop.
app_name = "myApp"
app = angular.module "#{app_name}.controllers", []
class BaseController
@register: (app, name) ->
name ?= @name || @toString().match(/function\s*(.*?)\(/)?[1]
app.controller name, @
@inject: (args...) ->
@$inject = args
constructor: (args...) ->
for key, index in @constructor.$inject
@[key] = args[index]
for key, fn in @constructor.prototype
continue unless typeof fn is 'function'
continue if key in ['constructor', 'initialize'] or key[0] is '_'
@$scope[key] = fn.bind?(@) || _.bind(fn, @)
@initialize?()
class TestController extends BaseController
@register app
@inject '$scope'
initialize: ->
@$scope.name = 'view 1'
@$scope.say = -> window.alert.apply window, arguments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment