项目作者: CocktailJS

项目描述 :
Traits with decorators
高级语言: JavaScript
项目地址: git://github.com/CocktailJS/traits-decorator.git
创建时间: 2015-06-20T01:26:40Z
项目社区:https://github.com/CocktailJS/traits-decorator

开源协议:MIT License

下载


traits-decorator

npm version
Build Status
bitHound Score

Experimental library to apply Traits with ES7 decorators.

Install

using npm

  1. npm i -S traits-decorator

using git repository

  1. npm i -S git://github.com/cocktailjs/traits-decorator

API

Decorators

@traits(Trait1, …TraitN)

Applicable to class definition. It will apply all the given Traits to the class.

  1. @traits(TExample) class MyClass {}

@requires(description1, …descriptionN)

Applicable to a method defined in a Trait. The decorator does nothing but it serves as a documentation to reflect what method / property the method needs access to.

  1. class TFoo {
  2. @requires('bar: {String}')
  3. fooBar() {
  4. console.log('foo,' + this.bar);
  5. }
  6. }

Bindings

excludes(Method1, …MethodN)

Applicable to Trait definition in ‘@traits’. It will exclude the given method names from the Trait.

  1. @traits(TExample::excludes('foo', 'bar'))
  2. class MyClass {}

alias(aliases: {})

Applicable to Trait definition in ‘@traits’. It will alias the method defined in the Trait with the key as the value .

  1. @traits(TExample::alias({baz: 'parentBaz'}))
  2. class MyClass {}

as({alias: {}, excludes: []})

Applicable to Trait definition in ‘@traits’. It will apply aliases and excluded methods from the Trait

  1. @traits( TExample::as({alias: {baz: 'parentBaz'}, excludes:['foo', 'bar'] }) )
  2. class MyClass {}

Usage

Basically, we have a few Traits (classes) TFirst, TLast and we combine and apply them by using traits decorator:

example.js

  1. 'use strict';
  2. import { traits, excludes, alias, requires } from 'traits-decorator'
  3. class TFirst {
  4. @requires('collection:[]')
  5. first() {
  6. return this.collection[0];
  7. }
  8. }
  9. class TLast {
  10. @requires('collection:[]')
  11. last() {
  12. let collection = this.collection;
  13. let l = collection.length;
  14. return collection[l-1];
  15. }
  16. justAnother() {}
  17. foo() {
  18. console.log('from TLast\'s foo');
  19. }
  20. }
  21. //composing a Trait with others
  22. @traits( TFirst, TLast::excludes('foo', 'justAnother') )
  23. class TEnum {
  24. foo() {
  25. console.log('enum foo')
  26. }
  27. }
  28. //apply trait TEnum
  29. @traits(TEnum::alias({ foo: 'enumFoo' }) )
  30. class MyClass {
  31. constructor (collection = []) {
  32. this.collection = collection
  33. }
  34. }
  35. let obj = new MyClass([1,2,3])
  36. console.log(obj.first()) // 1
  37. obj.enumFoo() // enum foo

In order to run the example.js we need babel and since we are using some experimental functionality, decorators (@traits) and bindOperator (::) we need to use the --stage 0.

  1. babel-node --stage 0 example.js

Update

@mixins decorator has been removed. If you want to use mixins please use mixins-decorator package.

License MIT