CalculatedValueListener

CalculatedValueListener

Represents an event listener which calculates field values. This listener is being registered for all data models.

A data field may have a calculation attribute. An instance of FunctionContext class will calculate this value by evaluating the expression provided.


 {
        "name": "modifiedBy",
        "title": "Modified By",
        "description": "Modified by user.",
        "type": "User",
        "calculation":"javascript:return this.user();"
    }
 

In the previous example modifiedBy field has a calculation for setting the user which performs the update operation.

Note:FunctionContext class may be extended in order to allow applications to perform value calculations.


    FunctionContext.prototype.myColor = function() {
        var deferred = Q.defer(),
            self = this;
        process.nextTick(function() {
            return self.context.model("UserColor")
                .where("user/name").equal(self.context.user.name)
                .select("color")
                .value().then(function(value) {
                    deferred.resolve(value);
                }).catch(function(err) {
                    deferred.reject(err);
                });
        });
        return deferred.promise;
    }
 

 {
        "name": "color",
        "title": "Color",
        "type": "Text",
        "calculation":"javascript:return this.myColor();"
    }
 

In this example a custom method of FunctionContext class gets the user's favourite color.

This calculation may also be performed by setting the following promise expression:


 {
        "name": "color",
        "title": "Color",
        "type": "Text",
        "calculation":"javascript:return this.context.model('UserColor').where('user/name').equal(this.context.user.name).select('color').value();"
    }
 

Constructor

new CalculatedValueListener()

Source:

Methods

beforeSave(e, callback)

Occurs before creating or updating a data object and calculates field values with the defined calculation expression.
Parameters:
Name Type Description
e DataEventArgs An object that represents the event arguments passed to this operation.
callback function A callback function that should be called at the end of this operation. The first argument may be an error if any occured.
Source: