Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
define [
], (BCtrl) ->
class AppController extends BCtrl
@register 'appController', [
# or (but then big inject deps could get long)
@register('appController').inject '$scope', '$location'
# or (too many arrays?)
@register('appController').inject [
# or (deals with whitespace issue too)
@register 'appController',
init: ->
@$ = "bar"
define [
], (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 [
], (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.
register: (cls, type, name, deps) ->
app = A.module cfg.ngApp
app[type] name || || cls.parseFnName(), cls
if _.isString deps
deps = (_.toArray arguments), 3
if _.isArray deps
cls.inject deps
inject: (cls, args...) ->
cls.$inject = _.toArray args
construct: (cls, args...) ->
_.forEach cls.$inject, (key, i) =>
cls[key] = args[i]
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