Skip to content
This repository was archived by the owner on Feb 2, 2018. It is now read-only.

Commit 93ba5a0

Browse files
committed
fixup! feedback applied
1 parent f04ff62 commit 93ba5a0

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

src/mixins/README.md

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@ LoopBack 4 supports mixins at an `Application` level. Your partial class can the
1010

1111
### High level example
1212
```ts
13-
const MixedClass = MyMixinClass(Application);
14-
const MultipleMixedClassesClass = MyMixinClass(MyMixinClass2(Application));
13+
class MyApplication extends MyMixinClass(Application) {
14+
// Your code
15+
};
16+
17+
// Multiple Classes mixed together
18+
class MyApp extends MyMixinClass(MyMixinClass2(Application)) {
19+
// Your code
20+
}
1521
```
1622

1723

@@ -25,11 +31,11 @@ For hello-extensions we write a simple Mixin that allows the `Application` class
2531
#### An example Logger
2632
```ts
2733
class ColorLogger implements Logger {
28-
log(...args: any[]) {
34+
log(...args: LogArgs) {
2935
console.log('log :', ...args);
3036
}
3137

32-
error(...args: any[]) {
38+
error(...args: LogArgs) {
3339
const data = args.join(' ');
3440
// log in red color
3541
console.log('\x1b[31m error: ' + data + '\x1b[0m');
@@ -47,15 +53,22 @@ It is also important for the constructor to call `super(args)` so `Application`
4753
```ts
4854
constructor(...args: any[]) {
4955
super(args);
50-
}```
56+
}
57+
```
5158

5259
### Binding via `ApplicationOptions`
5360
As mentioned earlier, since our `args` represents `ApplicationOptions`, we can make it possible for users to pass in their `Logger` implementations in a `loggers` array on `ApplicationOptions`. We can then read the array and automatically bind these for the user.
5461

5562
#### Example user experience
5663
```ts
57-
new LoggerMixin(Application)({
58-
loggers: [ColorLogger],
64+
class MyApp extends LoggerMixin(Application){
65+
constructor(...args: any[]) {
66+
super(...args);
67+
}
68+
};
69+
70+
const app = new MyApp({
71+
loggers: [ColorLogger]
5972
});
6073
```
6174

@@ -109,19 +122,15 @@ mountComponentLoggers(component: Constructor<any>) {
109122
class LoggingApplication extends LoggerMixin(Application) {
110123
constructor(...args: any[]) {
111124
super(...args);
112-
const app = this;
125+
this.logger(ColorLogger);
113126
}
114-
115-
app.logger(ColorLogger);
116127
}
117128
118129
// Binding a Logger provided by a component
119130
class MyComponent implements Component{
120131
loggers: [ColorLogger];
121132
}
122133
123-
const LoggingApplication = LoggerMixin(Application);
124-
125134
const app = new LoggingApplication({
126135
components: [MyComponent] // Logger from MyComponent will be bound to loggers.ColorLogger
127136
});

src/types.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
// tslint:disable-next-line:no-any
99
export type LogArgs = any[];
1010

11+
// A traditional Logger interface would have `.warn()` and `.info()` methods &
12+
// potentially others but they have been omitted to keep this example simple.
1113
export interface Logger {
1214
log(...args: LogArgs): void;
1315
error(...args: LogArgs): void;
14-
15-
info?(...args: LogArgs): void;
16-
warn?(...args: LogArgs): void;
1716
}

0 commit comments

Comments
 (0)