Created
March 1, 2015 00:05
-
-
Save idooo/f5ca8b740a7bfe1287be to your computer and use it in GitHub Desktop.
ES6 classes example for Angular directives
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 BasicDirective { | |
constructor(link, controller) { | |
if (typeof link !== 'undefined') this.link = link; | |
if (typeof controller !== 'undefined') this.controller = controller; | |
} | |
getClassName() { | |
var str = this.constructor.toString(); | |
str = str.substr('function '.length); | |
str = str.substr(0, str.indexOf('(')); | |
return str; | |
} | |
registerDirective(directiveName, moduleName) { | |
if (arguments.length < 2) { | |
moduleName = directiveName; | |
directiveName = this.getClassName(); | |
directiveName = directiveName.charAt(0).toLowerCase() + directiveName.slice(1); | |
} | |
angular | |
.module(moduleName) | |
.directive(directiveName, () => { return this }) | |
} | |
} | |
class Field extends BasicDirective { | |
constructor(link, controller) { | |
super(link, controller); | |
this.restrict = 'A'; | |
this.replace = true; | |
this.templateUrl = 'common/fields/field.template'; | |
this.scope = { | |
label: '@', | |
name: '@' | |
}; | |
} | |
} | |
class EmailField extends Field { | |
constructor(link, controller) { | |
super(link, controller); | |
this.scope.type = '@'; | |
this.templateUrl = 'common/fields/emailfield.template'; | |
} | |
} | |
new EmailField(link, controller).registerDirective('life.common'); | |
function link(scope) { | |
console.log(scope) | |
} | |
function controller($scope, $element, $attrs) { | |
console.log($scope) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment