Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@davej
Forked from elado/AngularBaseCtrl.coffee
Last active January 1, 2016 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save davej/8198036 to your computer and use it in GitHub Desktop.
Save davej/8198036 to your computer and use it in GitHub Desktop.
define [
'base-ctrl'
], (BaseCtrl) ->
class AppController extends BaseCtrl
@register()
@inject('$scope')
init: ->
@$scope.foo = "bar"
define [
'config'
'angular'
'lodash'
], (cfg, A, _) ->
class @BaseCtrl
@parseFnName: ->
# Parses the class name manually from the string of the function definition.
# We only do this if we are using a version of Coffeescript that doesn't have
# the function name defined on the `.name` attribute.
/\W*function\s+([\w\$]+)\(/.exec(@)[1]
@register: (name) ->
app = A.module cfg.ngApp
app.controller name || @name || @parseFnName(), @
@inject: (args...) ->
@$inject = args
constructor: (args...) ->
for key, index in @constructor.$inject
@[key] = args[index]
for key, fn of @constructor.prototype
continue unless typeof fn is 'function'
continue if key in ['constructor', 'init'] or key[0] is '_'
@$scope[key] = @constructor.prototype[key] = fn.bind?(@) || _.bind(fn, @)
@init?()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment