Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
define [
'controller/base-controller'
], (BCtrl) ->
class AppController extends BCtrl
@register 'appController', [
'$scope',
'$location'
]
# or (but then big inject deps could get long)
@register('appController').inject '$scope', '$location'
# or (too many arrays?)
@register('appController').inject [
'$scope',
'$location'
]
# or (deals with whitespace issue too)
@register 'appController',
'$scope',
'$location'
init: ->
@$scope.foo = "bar"
define [
'config'
'angular'
'lodash'
'helper/module-helper'
], (cfg, A, _, hlpr) ->
class BaseController
@register: (name, args...) ->
hlpr.register.apply @, [@, 'controller', name].concat _.toArray args
@inject: (args...) ->
hlpr.inject @, args
constructor: (args...) ->
hlpr.construct @, args
define [
'config'
'angular'
'lodash'
], (cfg, A, _) ->
helper =
parseFnName: (cls) ->
# 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(cls)[1]
register: (cls, type, name, deps) ->
app = A.module cfg.ngApp
app[type] name || cls.name || cls.parseFnName(), cls
if _.isString deps
deps = _.rest (_.toArray arguments), 3
if _.isArray deps
cls.inject deps
cls
inject: (cls, args...) ->
cls.$inject = _.toArray args
construct: (cls, args...) ->
_.forEach cls.$inject, (key, i) =>
cls[key] = args[i]
cls.init?()
bindAll: (cls) ->
_.forEach cls.constructor.prototype, (key, fn) ->
return unless typeof fn is 'function'
return if key in ['constructor', 'init'] or key[0] is '_'
cls.$scope[key] = cls.constructor.prototype[key] = fn.bind?(cls) || _.bind fn, cls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment