2016-01-21 80 views
1

我想知道是否可以通過JSONModel綁定事件。通過JSON模型綁定事件

如果我這樣做,它總是會拋出這樣的例外:

Uncaught TypeError: I.fFunction.call is not a function

這是我的代碼:

_ViewReference: undefined, 
_oMenuItemsConfigModel: undefined, 
createMenu: function(oItem){ 
    if (!this._menu) { 
     this._menu = new sap.ui.unified.Menu(this._oMenuConfig); 
     this._menu.setModel(this._oMenuItemsConfigModel); 

     this._menu.bindAggregation("items", "/", new sap.ui.unified.MenuItem({ 
      text: "{text}", 
      icon: "{icon}", 
      select: "{select}", 
      enabled: "{enabled}" 
     })); 

     this._ViewReference.addDependent(this._menu); 
    } 

    var eDock = sap.ui.core.Popup.Dock; 
    this._menu.open(false, oItem, eDock.BeginTop, eDock.BeginBottom, oItem); 
    return oItem; 
} 

我有一個通用的ContextMenu這只是需要一些配置,以獲得創建。這是我從我的控制器調用這個函數:

var oContextMenu = new ContextMenu(this.getView(), 
    new sap.ui.model.json.JSONModel(
     [ 
      { 
       text: "Copy", 
       select: [this.onContextMenuItemCopySelected, this] 
      }, 
      { 
       text: "Paste", 
       select: [this.onContextMenuItemPasteSelected, this] 
      } 
     ] 
    ) 
); 

這裏是一個JSBin例。

回答

1

您不能對事件使用數據綁定。 但是,您可以爲您的菜單項實現通用事件處理程序,該處理程序將調用相應的函數。

綁定到一個共同的事件處理程序的菜單項選擇事件:

this._menu.bindAggregation("items", "/", new sap.ui.unified.MenuItem({ 
       text: "{text}", 
       select: [this.onSelect, this] 
      })); 

而實現這樣的處理程序:

onSelect:function(oEvent){ 
      var item = oEvent.getParameter("item"); 
      var context = item.getBindingContext(); 
      var fnConfig = context.getProperty("select"); 
      fnConfig[0].bind(fnConfig[1])(); 

     } 

fnConfig是此對象的功能的,從模型中的數組。 使用Function.bind()可讓您調用給定此對象上的函數。

這是在JSBin

+0

它在JSBin中工作,不是嗎?請注意,我在JSBin示例中添加了'onContextMenuItemCopySelected'和'onContextMenuItemPasteSelected'函數給控制器。你的真實代碼中的JSBin示例有一些不同嗎? – schnoedel

+0

不當然,它的作品!但我真的不明白爲什麼它不能與綁定工作!不過,這是一個很好的工作。 –

+0

SAP僅對屬性關聯和聚合實施了數據綁定。它全部在[ManagedObject](https://sapui5.netweaver.ondemand.com/#docs/api/symbols/sap.ui.base.ManagedObject.html)中。我想他們假設模型內容總是隻有數據。從一個真實的web服務加載 - 但你的榜樣是一個很好的反例。 – schnoedel