Update events.md
This commit is contained in:
parent
d8114d53f5
commit
fbf1752a00
@ -1,20 +1,20 @@
|
||||
# 事件
|
||||
# Events
|
||||
|
||||
NocoBase 在应用、插件、数据库的生命周期中提供了非常多的事件监听,这些方法只有在触发了事件之后才会执行。
|
||||
NocoBase provides a very large number of event listeners in the lifecycle of applications, plugins, and database, and these methods will only be executed when an event is triggered.
|
||||
|
||||
## 如何添加事件监听?
|
||||
## How to add event listeners?
|
||||
|
||||
事件的注册一般放于 afterAdd 或 beforeLoad 中
|
||||
The registration of events is usually placed in afterAdd or beforeLoad
|
||||
|
||||
```ts
|
||||
export class MyPlugin extends Plugin {
|
||||
// 插件添加进来之后,有没有激活都执行 afterAdd()
|
||||
// After the plugin is added, afterAdd() is executed with or without activation
|
||||
afterAdd() {
|
||||
this.app.on();
|
||||
this.db.on();
|
||||
}
|
||||
|
||||
// 只有插件激活之后才会执行 beforeLoad()
|
||||
// beforeLoad() will only be executed after the plugin is activated
|
||||
beforeLoad() {
|
||||
this.app.on();
|
||||
this.db.on();
|
||||
@ -24,7 +24,7 @@ export class MyPlugin extends Plugin {
|
||||
|
||||
### `db.on`
|
||||
|
||||
数据库相关事件与 Collection 配置、Repository 的 CRUD 相关,包括:
|
||||
Database related events are related to Collection configuration, CRUD of Repository, including:
|
||||
|
||||
- 'beforeSync' / 'afterSync'
|
||||
- 'beforeValidate' / 'afterValidate'
|
||||
@ -37,13 +37,13 @@ export class MyPlugin extends Plugin {
|
||||
- 'afterSaveWithAssociations'
|
||||
- 'beforeDefineCollection'
|
||||
- 'afterDefineCollection'
|
||||
- 'beforeRemoveCollection' / 'afterRemoveCollection
|
||||
- 'beforeRemoveCollection' / 'afterRemoveCollection'
|
||||
|
||||
更多详情参考 [Database API](/api/database#内置事件)。
|
||||
See [Database API](/api/database) for more details.
|
||||
|
||||
### `app.on()`
|
||||
|
||||
app 的事件与应用的生命周期相关,相关事件有:
|
||||
The app's events are related to the application's lifecycle, and the relevant events are:
|
||||
|
||||
- 'beforeLoad' / 'afterLoad'
|
||||
- 'beforeInstall' / 'afterInstall'
|
||||
@ -52,15 +52,15 @@ app 的事件与应用的生命周期相关,相关事件有:
|
||||
- 'beforeStop' / 'afterStop'
|
||||
- 'beforeDestroy' / 'afterDestroy'
|
||||
|
||||
更多详情参考 [Application API](/api/server/application#事件)。
|
||||
Refer to [Application API](/api/server/application#Events) for more details.
|
||||
|
||||
## 示例
|
||||
## Example
|
||||
|
||||
我们继续以简单的在线商店来举例,相关的数据表建模可以回顾 [数据表和字段](/development/) 部分的示例。
|
||||
Let's continue with a simple online store as an example, the related collections modeling can be reviewed in the [Collections and Fields](/development/) section for examples.
|
||||
|
||||
### 创建订单后减商品库存
|
||||
### Deducting product inventory after creating an order
|
||||
|
||||
通常我们的商品和订单是不同的数据表,而客户在下单以后把商品的库存减掉可以解决超卖的问题,这时候我们可以针对创建订单这个数据操作定义相应的事件,在这个时机一并解决库存修改的问题:
|
||||
Usually we have different collections for products and orders. The problem of overselling can be solved by subtracting the inventory of the item after the customer has placed the order. At this point we can define the corresponding event for the action of Creating Order and solve the inventory modification problem at this time together with:
|
||||
|
||||
```ts
|
||||
class ShopPlugin extends Plugin {
|
||||
@ -80,9 +80,9 @@ class ShopPlugin extends Plugin {
|
||||
}
|
||||
```
|
||||
|
||||
因为默认 Sequelize 的事件中就携带事务等信息,所以我们可以直接使用 transaction 以保证两个数据操作都在同一事务中进行。
|
||||
Since the default Sequelize event carries information about the transaction, we can use transaction directly to ensure that both data actions are performed in the same transaction.
|
||||
|
||||
同样的,也可以在创建发货记录后修改订单状态为已发货:
|
||||
Similarly, you can change the order status to shipped after creating the shipping record: ```ts
|
||||
|
||||
```ts
|
||||
class ShopPlugin extends Plugin {
|
||||
@ -101,9 +101,9 @@ class ShopPlugin extends Plugin {
|
||||
}
|
||||
```
|
||||
|
||||
### 随应用同时存在的定时任务
|
||||
### Timed tasks that exist alongside the application
|
||||
|
||||
在不考虑使用工作流插件等复杂情况下,我们也可以通过应用级的事件实现一个简单的定时任务机制,且可以与应用的进程绑定,退出后就停止。比如我们希望定时扫描所有订单,超过签收时间后自动签收:
|
||||
Without considering complex cases such as using workflow plugins, we can also implement a simple timed task mechanism via application-level events, and it can be bound to the application's process and stop when it exits. For example, if we want to scan all orders at regular intervals and automatically sign them after the sign-off time.
|
||||
|
||||
```ts
|
||||
class ShopPlugin extends Plugin {
|
||||
@ -146,7 +146,7 @@ class ShopPlugin extends Plugin {
|
||||
|
||||
load() {
|
||||
this.app.on('beforeStart', () => {
|
||||
// 每分钟执行一次
|
||||
// execute every minute
|
||||
this.timer = setInterval(this.checkOrder, 1000 * 60);
|
||||
});
|
||||
|
||||
@ -158,11 +158,11 @@ class ShopPlugin extends Plugin {
|
||||
}
|
||||
```
|
||||
|
||||
## 小结
|
||||
## Summary
|
||||
|
||||
通过上面的示例,我们基本了解了事件的作用和可以用于扩展的方式:
|
||||
The above example gives us a basic understanding of what events do and the ways they can be used to extend.
|
||||
|
||||
* 数据库相关的事件
|
||||
* 应用相关的事件
|
||||
* Database related events
|
||||
* Application related events
|
||||
|
||||
本章涉及的示例代码整合在对应的包 [packages/samples/shop-events](https://github.com/nocobase/nocobase/tree/main/packages/samples/shop-events) 中,可以直接在本地运行,查看效果。
|
||||
The sample code covered in this chapter is integrated in the corresponding package [packages/samples/shop-events](https://github.com/nocobase/nocobase/tree/main/packages/samples/shop-events), which can be run directly in run locally to see the results.
|
||||
|
Loading…
Reference in New Issue
Block a user