-
-
Save ahmednuaman/8200701 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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