Jan Koester
Jan Koester

Reputation: 1218

SAPUI5 local scope object as formatter function parameter

How can local scope objects pass as parameter into formatter functions. I only know how to pass values from the binding into the formatter function but I need to pass additional objects from local scope into the formatter function.

            // request the order operations and bind them to the operation list
            oView.getModel().read(sPath + "/OperationSet", {
                success: function (oData, oResponse) {
                    if (oData && oData.results) {
                        var oOrder = oView.getBindingContext().getObject();
                        // I need sOrderType inside the external formatter function
                        var sOrderType = oOrder.OrderType;

                        operationList.bindAggregation("items", {
                            path: sPath + "/OperationSet",
                            template: new sap.m.StandardListItem({
                                title: "{Description}",
                                info: "{DurationNormal} {DurationNormalUnit}",
                                visible: {
                                    parts: [{path: 'Activity'}],
                                    formatter: de.example.util.Formatter.myFormatterFunction
                                }
                            }),
                            filters: []
                        });
                    }
                },
                error: function (oData) {
                    jQuery.sap.log.error("Could not read order operations");
                }
            });

Upvotes: 0

Views: 3979

Answers (2)

Daniël Camps
Daniël Camps

Reputation: 1809

you can simply call the local object from inside your formatter context.

Say you have:

var sString = "Test";

oTxt.bindValue({ parts: [{
    path: "/firstName",
    type: new sap.ui.model.type.String()
        }, {
    path: "/lastName",
    type: new sap.ui.model.type.String()
        }, {
    path: "/amount",
    type: new sap.ui.model.type.Float()
        }, {
    path: "/currency",
    type: new sap.ui.model.type.String()
        }], formatter: function (firstName, lastName, amount, currency) { 

console.log(sString);

// all parameters are strings 
    if (firstName && lastName) {
        return "Dear " + firstName + " " + lastName + ". Your current balance is: " + amount + " " + currency;
    } else {
        return null;
    } } });

This will properly log the sString as "test".

If you are talking about a variable that is local in a completely different place (e.g. a different view or controller), then you have to be more specific about from where to where you want your variable in order to get the best answer.

Upvotes: 1

Tiago A.
Tiago A.

Reputation: 1482

You can:

Also bind to other values that contain the same information as the one in oMyObject

Create a closure to capture oMyObject and make it available inside the formatter. e.g.:

 formatter: ((function(oObject){
                //oObject is available inside this function
                return function (firstName, lastName, amount, currency) { // all parameters are strings 
                    if (firstName && lastName && oObject.someProperty == null) {
                        return "Dear " + firstName + " " + lastName + ". Your current balance is: " + amount + " " + currency;
                    } else {
                        return null;
                    }
                };
            })(oMyObject))

Store the information in oMyObject somewhere in the model and access that model inside the formatter function (although a bit of an antipattern)

Upvotes: 2

Related Questions