most-data/types~ DataModelEventListener

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" }

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.
// 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
        .subject("New Order")
        .template("new-order").send(, function(err) {
        if (err) { return web.common.log(err); }
        return callback();