2015-06-12 51 views
0

本地作用域對象如何作爲參數傳遞到格式化程序函數中。我只知道如何將綁定值傳遞給格式化函數,但我需要將本地範圍的其他對象傳遞給格式化函數。SAPUI5本地作用域對象作爲格式化程序函數參數

  // 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"); 
       } 
      }); 
+0

oMyObject是恆定的,與綁定值無關嗎? –

+0

沒有oMyObject與其他視圖信息一起創建。 –

回答

2

您可以:

也結合到包含相同的信息之一oMyObject

創建一個封閉捕獲oMyObject,並使其可格式化內的其他值。例如:

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)) 

存放在oMyObject信息某處模型和對格式化函數內部的模型(雖然有點反模式)

1

,你可以簡單地從你的格式化上下文中調用本地對象。

假設你有:

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; 
    } } }); 

這將正確登錄的sString爲 「測試」。

如果你正在談論的是一個完全不同的地方(例如不同的視圖或控制器)的本地變量,那麼你必須更具體地說明從哪裏到哪裏你想要變量以獲得最好的回答。

+0

如果我在同一個功能塊中嵌入格式化函數,這個結論只會起作用。我調整我的源代碼來表示問題。我給你+1,因爲解決方案適用於我以前的示例代碼 –

+0

在這種情況下,我將執行以下操作:在更改「sOrderType」時,將此作爲屬性添加到您的模型中(oModel.setProperty(sNewPath,sOrderType )); 在你的格式化函數中綁定部分(例如這個答案),其中一個路徑被設置爲你新設置的屬性。每次屬性更改時,格式化程序功能都會自動更新。當沒有設置時,字符串將返回undefined,否則字符串將返回您的字符串值。 –