Skip to content

Instantly share code, notes, and snippets.

@vyorkin
Created February 24, 2014 16:28
Show Gist options
  • Save vyorkin/9191604 to your computer and use it in GitHub Desktop.
Save vyorkin/9191604 to your computer and use it in GitHub Desktop.
class @Base
constructor: (args...) ->
for key, index in @constructor.$inject
@[key] = args[index]
@inject: (args...) -> @$inject = _.union @$inject args
class @BaseCtrl extends @Base
constructor: (args...) ->
super args...
@_bindScopeMethods
@initialize?()
@register: (app, kind = 'controller', name) ->
name ?= @name || @toString().match(/function\s*(.*?)\(/)?[1]
app.controller name, @
@_bindScopeMethods: ->
for key, fn in @constructor.$inject
continue unless @_bindable?(key, fn)
@$scope[key] = fn.bind?(@) || _.bind(fn, @)
@_function?: (fn) -> fn is 'function'
@_bindable?: (key, fn) -> @_isFunction(fn) && !@_isPrivate?(key)
@_private?: (name) -> name in ['constructor', 'initialize'] || name[0] is '_'
class TestController extends @BaseCtrl
@register app
@inject '$scope'
initialize: ->
@$scope.name = 'view 1'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment