Skip to content
This repository has been archived by the owner on Aug 22, 2022. It is now read-only.

Commit

Permalink
- @InjectAsProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
Maksym Butsykin committed Aug 12, 2016
1 parent 287f7ba commit 6673625
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 8 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ npm install angular-es
- [@Factory](#factory)
- [@Filter](#filter)
- [@Inject](#inject)
- ~~[@InjectAsProperty](#injectAsProperty)~~
- [@InjectAsProperty](#injectAsProperty)
- [@Module](#module)
- [@Provider](#provider)
- [@Run](#run)
Expand Down Expand Up @@ -222,10 +222,10 @@ import {Inject} from 'angular-es';
class BaseInjectedClass {
}

@Inject('$http')
@Inject('$http', '$q')
class InjectedClass extends BaseInjectedClass {

constructor($rootScope, $http) {
constructor($rootScope, $http, $q) {
super($rootScope);
}

Expand All @@ -239,6 +239,22 @@ class InjectedClass extends BaseInjectedClass {
}
```

### InjectAsProperty
Injects provided dependencies as properties

```javascript
import {Module, Service, InjectAsProperty} from 'angular-es';

@Module(test.name)
@Service('testService')
@InjectAsProperty('$q', '$http')
class TestService {
testMethod() {
return this.$http();
}
}
```

### Module
Attaches target to specified angular module
<br />
Expand Down
4 changes: 3 additions & 1 deletion src/main/ES/decorators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Decorator from './decorator.decorator';
import Factory from './factory.decorator';
import Filter from './filter.decorator';
import Provider from './provider.decorator';
import InjectAsProperty from './injectAsProperty.decorator';

export {
Module,
Expand All @@ -27,5 +28,6 @@ export {
Decorator,
Factory,
Filter,
Provider
Provider,
InjectAsProperty
};
16 changes: 16 additions & 0 deletions src/main/ES/decorators/injectAsProperty.decorator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function injectAsProperty(...dependencies) {
return target => {
target.injectAsProperty = target.injectAsProperty || [];

dependencies.forEach(dependency => {
target.injectAsProperty.push({
key: dependency,
value: dependency
});
});

return target;
}
}

export default injectAsProperty;
22 changes: 22 additions & 0 deletions src/main/ES/decorators/module.decorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,30 @@ function module(name) {

module[target.ngType].apply(module, target.ngArguments);

if (target.injectAsProperty) {
console.log(target.injectAsProperty);
provideInjections(module, target);
}

return target;
};

function provideInjections(module, target) {
module
.config(['$provide', $provide => {
$provide
.decorator(target.ngName, [
'$delegate', '$injector',
($delegate, $injector) => {
target.injectAsProperty.forEach(dependency => {
$delegate[dependency.key] = $injector.get(dependency.value);
});

return $delegate;
}
]);
}])
}
}

export default module;
4 changes: 3 additions & 1 deletion src/main/ES/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
Decorator,
Factory,
Filter,
Provider
Provider,
InjectAsProperty
} from './decorators';

import {
Expand All @@ -37,6 +38,7 @@ export {
Factory,
Filter,
Provider,
InjectAsProperty,

// helpers
getInjectableClass,
Expand Down
2 changes: 1 addition & 1 deletion src/test/ES/decorators/inject.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {BaseInjectedClass, InjectedClass} from '../mock/inject';
import {BaseInjectedClass, InjectedClass} from '../mock/test.inject';

describe('@Inject', () => {
it('decorates class', () => {
Expand Down
19 changes: 19 additions & 0 deletions src/test/ES/decorators/injectAsProperty.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {TestService} from '../mock/test.InjectAsProperty';

describe('@InjectAsProperty', () => {

let testService,
$q,
$http;

beforeEach(angular.mock.inject((_testService_, _$q_, _$http_) => {
testService = _testService_;
$q = _$q_;
$http = _$http_;
}));

it('injects dependencies', () => {
expect(testService.$q).toEqual($q);
expect(testService.$http).toEqual($http);
});
});
6 changes: 4 additions & 2 deletions src/test/ES/mock/decorators.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
Decorator,
Factory,
Filter,
Provider
Provider,
InjectAsProperty
} from '../../../main/ES';

export {
Expand All @@ -29,5 +30,6 @@ export {
Decorator,
Factory,
Filter,
Provider
Provider,
InjectAsProperty
};
10 changes: 10 additions & 0 deletions src/test/ES/mock/test.InjectAsProperty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {test} from './test.module';
import {Module, Service, InjectAsProperty} from './decorators';

@Module(test.name)
@Service('testService')
@InjectAsProperty('$q', '$http')
class TestService {
}

export {TestService};
File renamed without changes.

0 comments on commit 6673625

Please sign in to comment.