Page Contents
This is a community translation into Chinese. For the latest information, see the English version.
Related articles:
除了标准的Node事件, LoopBack应用和模型会emit一些其他的事件。
应用事件
默认情况下,脚手架应用在启动后并运行完启动脚本后会触发一个stared事件。
模型事件
The following table summarizes the events that LoopBack models can emit.
Event | Emitted when... | Arguments | Argument type | Class methods that emit | Instance methods that emit |
---|---|---|---|---|---|
'attached' |
Model is attached to an app.
|
Model class | Object | app.model(modelName) | |
'changed' | Model instance is created, saved, or updated. | Model instance | Object |
|
|
'dataSourceAttached' | Model is attached to a Data source. | Model class | Object |
|
|
'deleted' | Model instance is deleted. | Model ID | Number |
|
|
'deletedAll' | Model instance is deleted. | where (optional) | JSON object |
|
|
'set' | Model property is set. | Model instance | Object |
Model.prototype.setAttributes() |
changed
在一个模型成功创建,保存,更新后触发。参数:inst
, 是模型实例对象。例如:
MyModel.on('changed', function(inst) {
console.log('model with id %s has been changed', inst.id);
// => model with id 1 has been changed
});
deleted
在单个模型被删除后触发。参数:id,是模型实例的id。例如:
MyModel.on('deleted', function(id) {
console.log('model with id %s has been deleted', id);
// => model with id 1 has been deleted
});
deletedAll
多个模型实例被删除后触发。参数: where
(可选), 是where过滤器,一个JSON对象。例如:
MyModel.on('deletedAll', function(where) {
if (where) {
console.log('all models where ', where, ' have been deleted');
// => all models where
// => {price: {gt: 100}}
// => have been deleted
}
});
attached
模型被attach到app之后触发。
dataSourceAttached
模型被attach到DataSource后触发。
set
模型属性被设置后触发。参数:inst
, 一个模型实例。例如:
MyModel.on('set', function(inst) {
console.log('model with id %s has been changed', inst.id);
// => model with id 1 has been changed
});
用户模型事件
其他的事件:
User.resetPassword()
触发’resetPasswordRequest’ 事件。
PersistedModel 事件
PersistedModel有一个changed事件用来监听模型的改变。例如:
MyPersistedModel.on('changed', function(obj) {
console.log(obj) // => the changed model
});