new DataModelEventListener()
A data model uses event listeners as triggers which are automatically executed after data operations. Those listeners are defined in [eventListeners] section of a model's schema.
{
...
"fields": [ ... ],
...
"eventListeners": [
{ "name":"Update Listener", "type":"/app/controllers/an-update-listener.js" },
{ "name":"Another Update Listener", "type":"module-a/lib/listener" }
]
...
}
Properties:
Name | Type | Description |
---|---|---|
name |
string | Gets or sets a short description for this listener |
type |
string | Gets or sets a string which is the path of the module that exports this listener. |
disabled |
boolean | Gets or sets a boolean value that indicates whether this listener is disabled or not. The default value is false. |
- Source:
Example
// A simple DataEventListener that sends a message to sales users after new order was arrived.
var web = require("most-web");
exports.afterSave = function(event, callback) {
//exit if state is other than [Insert]
if (event.state != 1) { return callback() }
//initialize web mailer
var mm = require("most-web-mailer"), context = event.model.context;
//send new order mail template by passing new item data
mm.mailer(context).to("sales@example.com")
.cc("supervisor@example.com")
.subject("New Order")
.template("new-order").send(event.target, function(err) {
if (err) { return web.common.log(err); }
return callback();
});
};