Created
May 19, 2014 13:02
-
-
Save lucassus/80ad1360ccad990d79e4 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
class @BaseCtrl | |
@register: (app, name) -> | |
name ?= @name || @toString().match(/function\s*(.*?)\(/)?[1] | |
app.controller name, this | |
@inject: (annotations...) -> | |
ANNOTATION_REG = /^(\S+)(\s+as\s+(\w+))?$/ | |
@annotations = _.map annotations, (annotation) -> | |
match = annotation.match(ANNOTATION_REG) | |
name: match[1], identifier: match[3] or match[1] | |
@$inject = _.map @annotations, (annotation) -> annotation.name | |
constructor: (dependencies...) -> | |
for annotation, index in @constructor.annotations | |
this[annotation.identifier] = dependencies[index] | |
@initialize?() |
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
app = angular.module("myApp") | |
class FormCtrl extends BaseCtrl | |
@register app, "products.FormCtrl" | |
@inject "$scope", "$location", "alerts", "product as remote" | |
initialize: -> | |
@reset() | |
save: (product) -> | |
promise = product.$save() | |
successMessage = if product.persisted() then "Product was updated" else "Product was created" | |
promise.then => | |
@alerts.success successMessage | |
@$location.path "/products" | |
reset: -> | |
@product = angular.copy(@remote) | |
@$scope.product = @product | |
delete: -> | |
promise = @product.$delete() | |
promise.then => | |
@alerts.info "Product was deleted" | |
@$location.path "/products" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
+1 Very smart approach ;)
If we omit
@inject "$http" ...
when we extend the base controller, we get Cannot read property 'length' of undefined.I patched my version where I check in the base constructor for dependencies.length before start iterating