mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-25 03:02:21 +00:00
docs(decorator): edit decorator/publishEvent
This commit is contained in:
parent
eeba1984a8
commit
dad929d01e
@ -476,15 +476,23 @@ person.facepalmWithoutWarning();
|
|||||||
我们可以使用修饰器,使得对象的方法被调用时,自动发出一个事件。
|
我们可以使用修饰器,使得对象的方法被调用时,自动发出一个事件。
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
import postal from "postal/lib/postal.lodash";
|
const postal = require("postal/lib/postal.lodash");
|
||||||
|
|
||||||
export default function publish(topic, channel) {
|
export default function publish(topic, channel) {
|
||||||
|
const channelName = channel || '/';
|
||||||
|
const msgChannel = postal.channel(channelName);
|
||||||
|
msgChannel.subscribe(topic, v => {
|
||||||
|
console.log('频道: ', channelName);
|
||||||
|
console.log('事件: ', topic);
|
||||||
|
console.log('数据: ', v);
|
||||||
|
});
|
||||||
|
|
||||||
return function(target, name, descriptor) {
|
return function(target, name, descriptor) {
|
||||||
const fn = descriptor.value;
|
const fn = descriptor.value;
|
||||||
|
|
||||||
descriptor.value = function() {
|
descriptor.value = function() {
|
||||||
let value = fn.apply(this, arguments);
|
let value = fn.apply(this, arguments);
|
||||||
postal.channel(channel || target.channel || "/").publish(topic, value);
|
msgChannel.publish(topic, value);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -495,29 +503,37 @@ export default function publish(topic, channel) {
|
|||||||
它的用法如下。
|
它的用法如下。
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
import publish from "path/to/decorators/publish";
|
// index.js
|
||||||
|
import publish from './publish';
|
||||||
|
|
||||||
class FooComponent {
|
class FooComponent {
|
||||||
@publish("foo.some.message", "component")
|
@publish('foo.some.message', 'component')
|
||||||
someMethod() {
|
someMethod() {
|
||||||
return {
|
return { my: 'data' };
|
||||||
my: "data"
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
@publish("foo.some.other")
|
@publish('foo.some.other')
|
||||||
anotherMethod() {
|
anotherMethod() {
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let foo = new FooComponent();
|
||||||
|
|
||||||
|
foo.someMethod();
|
||||||
|
foo.anotherMethod();
|
||||||
```
|
```
|
||||||
|
|
||||||
以后,只要调用`someMethod`或者`anotherMethod`,就会自动发出一个事件。
|
以后,只要调用`someMethod`或者`anotherMethod`,就会自动发出一个事件。
|
||||||
|
|
||||||
```javascript
|
```bash
|
||||||
let foo = new FooComponent();
|
$ bash-node index.js
|
||||||
|
频道: component
|
||||||
|
事件: foo.some.message
|
||||||
|
数据: { my: 'data' }
|
||||||
|
|
||||||
foo.someMethod() // 在"component"频道发布"foo.some.message"事件,附带的数据是{ my: "data" }
|
频道: /
|
||||||
foo.anotherMethod() // 在"/"频道发布"foo.some.other"事件,不附带数据
|
事件: foo.some.other
|
||||||
|
数据: undefined
|
||||||
```
|
```
|
||||||
|
|
||||||
## Mixin
|
## Mixin
|
||||||
|
Loading…
x
Reference in New Issue
Block a user