8000 Return $delegate on decoration and add readme examples · DAB0mB/angular-ecmascript@6976ec6 · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit 6976ec6

Browse files
committed
Return $delegate on decoration and add readme examples
1 parent 9fac833 commit 6976ec6

File tree

3 files changed

+68
-22
lines changed

3 files changed

+68
-22
lines changed

README.md

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,14 @@ Used to define a new [service](https://docs.angularjs.org/guide/services).
9292
import { Service } from 'angular-ecmascript/module-helpers';
9393

9494
class DateService extends Service {
95-
$name = '$date'
95+
static $name = '$date'
9696

9797
now() {
9898
return new Date().getTime();
9999
}
100100
}
101101
```
102+
102103
### Factory
103104

104105
Used to define a new `factory`.
@@ -109,7 +110,7 @@ Used to define a new `factory`.
109110
import { Factory } from 'angular-ecmascript/module-helpers';
110111

111112
class MomentFactory extends Factory {
112-
$name = 'now'
113+
static $name = 'now'
113114

114115
create() {
115116
return new Date().getTime();
@@ -166,6 +167,7 @@ Used to define a new [decorator](https://docs.angularjs.org/guide/decorators).
166167

167168
- `$delegate` will be injected automatically so no need to specify it.
168169
- Note that the `decorate` method must be implemented, otherwise an error will be thrown during load time.
170+
- No need to return the `$delegate` object, it should be handled automatically.
169171

170172
```js
171173
import { Decorator } from 'angular-ecmascript/module-helpers';
@@ -179,7 +181,6 @@ class MyDecorator extends Decorator {
179181

180182
decorate() {
181183
this.$delegate.aHelpfulAddition = this.helperFn;
182-
return this.$delegate;
183184
}
184185
}
185186
```
@@ -217,12 +218,58 @@ Used to define a new `config`.
217218

218219
- Note that the `configure` method must be implemented, otherwise an error will be thrown during load time.
219220

221+
```js
222+
import { Config } from 'angular-ecmascript/module-helpers';
223+
224+
class RoutesCfg extends Config {
225+
static $inject = ['$routeProvider']
226+
227+
constructor(...args) {
228+
super(...args);
229+
230+
this.fetchUser = ['http', this::this.fetchUser];
231+
}
232+
233+
configure() {
234+
this.$routeProvider
235+
.when('/', {
236+
template: '<home user="$resolve.user"></home>',
237+
resolve: {
238+
user: this.fetchUser
239+
}
240+
});
241+
}
242+
243+
fetchUser($http) {
244+
return $http.get('...');
245+
}
246+
}
247+
```
248+
220249
### Runner
221250

222251
Used to define a new `run block`.
223252

224253
- Note that the `run` method must be implemented, otherwise an error will be thrown during load time.
225254

255+
```js
256+
import { Runner } from 'angular-meteor/module-helpers';
257+
258+
class RoutesRunner extends Runner {
259+
static $inject = ['$rootScope', '$state']
260+
261+
run() {
262+
this.$rootScope.$on('$stateChangeError', (...args) => {
263+
const [,,, err] = args;
264+
265+
if (err === 'AUTH_REQUIRED') {
266+
this.$state.go('login');
267+
}
268+
});
269+
}
270+
}
271+
```
272+
226273
## Download
227274

228275
The source is available for download from [GitHub](http://github.com/DAB0mB/angular-ecmascript). Alternatively, you can install using Node Package Manager (`npm`):

src/module-loader.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export default class Loader {
6767
}
6868

6969
loadDirective(Directive) {
70-
function helper(...args) {
70+
const helper = (...args) => {
7171
return new Directive(...args);
7272
}
7373

@@ -76,12 +76,18 @@ export default class Loader {
7676
}
7777

7878
loadDecorator(Decorator) {
79-
function helper(...args) {
80-
const decorator = new Decorator(...args);
81-
return decorator::decorator.decorate;
79+
let decorator;
80+
const $inject = Decorator.$inject;
81+
82+
const helper = (...args) => {
83+
decorator = new Decorator(...args);
84+
return decorate;
8285
}
8386

84-
const $inject = Decorator.$inject;
87+
const decorate = (...args) => {
88+
decorator.decorate(...args);
89+
return decorator.$delegate;
90+
}
8591

8692
if (!Utils.hasValue($inject, '$delegate')) {
8793
$inject.unshift('$delegate');
@@ -91,7 +97,7 @@ export default class Loader {
9197
}
9298

9399
loadFactory(Factory) {
94-
function helper(...args) {
100+
const helper = (...args) => {
95101
const factory = new Factory(...args);
96102
return factory::factory.create;
97103
}
@@ -101,7 +107,7 @@ export default class Loader {
101107
}
102108

103109
loadFilter(Filter) {
104-
function helper(...args) {
110+
const helper = (...args) => {
105111
const filter = new Filter(...args);
106112
return filter::filter.filter;
107113
}
@@ -111,7 +117,7 @@ export default class Loader {
111117
}
112118

113119
loadConfig(Config) {
114-
function helper(...args) {
120+
const helper = (...args) => {
115121
const config = new Config(...args);
116122
return config.configure();
117123
}
@@ -121,7 +127,7 @@ export default class Loader {
121127
}
122128

123129
loadRunner(Runner) {
124-
function helper(...args) {
130+
const helper = (...args) => {
125131
const runner = new Runner(...args);
126132
return runner.run();
127133
}

test/src/specs/module-helpers/decorator.spec.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ describe('Decorator', function() {
77

88
it('should load a decorator helper', function() {
99
const $delegate = {};
10-
const expectedResult = {};
1110
const ctorSpy = jasmine.createSpy('ctor');
1211
const methodSpy = jasmine.createSpy('method');
1312

@@ -21,7 +20,6 @@ describe('Decorator', function() {
2120

2221
decorate(...args) {
2322
this::methodSpy(...args);
24-
return expectedResult;
2523
}
2624
}
2725

@@ -40,12 +38,11 @@ describe('Decorator', function() {
4038
expect(expectedDecorator).toEqual(jasmine.any(Decorator));
4139
expect(decorator).toEqual(expectedDecorator);
4240
expect(decorator.$delegate).toEqual($delegate);
43-
expect(result).toEqual(expectedResult);
41+
expect(result).toEqual($delegate);
4442
});
4543

4644
it('should load a decorator with a custom name', function() {
4745
const $delegate = {};
48-
const expectedResult = {};
4946
const ctorSpy = jasmine.createSpy('ctor');
5047
const methodSpy = jasmine.createSpy('method');
5148

@@ -60,7 +57,6 @@ describe('Decorator', function() {
6057

6158
decorate(...args) {
6259
this::methodSpy(...args);
63-
return expectedResult;
6460
}
6561
}
6662

@@ -78,14 +74,12 @@ describe('Decorator', function() {
7874

7975
expect(expectedDecorator).toEqual(jasmine.any(TestDecorator));
8076
expect(decorator).toEqual(expectedDecorator);
81-
expect(decorator.$delegate).toEqual($delegate);
82-
expect(result).toEqual(expectedResult);
77+
expect(result).toEqual($delegate);
8378
});
8479

8580
it('should inject a `$delegate` dependency by default', function() {
8681
const $delegate = {};
8782
const $dep = {};
88-
const expectedResult = {};
8983
const ctorSpy = jasmine.createSpy('ctor');
9084
const methodSpy = jasmine.createSpy('method');
9185

@@ -99,7 +93,6 @@ describe('Decorator', function() {
9993

10094
decorate(...args) {
10195
this::methodSpy(...args);
102-
return expectedResult;
10396
}
10497
}
10598

@@ -119,6 +112,6 @@ describe('Decorator', function() {
119112
expect(expectedDecorator).toEqual(jasmine.any(TestDecorator));
120113
expect(decorator).toEqual(expectedDecorator);
121114
expect(decorator.$dep).toEqual($dep);
122-
expect(result).toEqual(expectedResult);
115+
expect(result).toEqual($delegate);
123116
});
124117
});

0 commit comments

Comments
 (0)
0