2014-06-20 38 views
2

我有一個SAPUI5分割應用程序,包含主視圖和詳細視圖。SAPUI5獲取當前上下文的詳細視圖

當我選擇在邊欄的項目,我通過上下文來詳細視圖,讓說產品1

onSelectProduct: function(evt){ 
    sap.ui.getCore().getEventBus().publish("app", "refreshProductDetail", {context : evt.getSource().getBindingContext()}); 

}, 

這觸發以下結合所述上下文功能:

refresh: function(channelId, eventId, data){ 
    if (data && data.context) { 
     this.getView().setBindingContext(data.context); 
    } 
}, 

現在,當我執行保存操作時,我想要獲取模型中產品1的當前數據。

然而,當我使用

this.getView().getBindingContext().getModel() 

它返回所有的產品模型。我如何知道用戶正在查看哪一個?

回答

12

可以使用getPath()一個BindingContext中,看到正在顯示的內容對象:

this.getView().getBindingContext().getPath(); 

你可以做這樣的事情:

var bindingContext = this.getView().getBindingContext(); 
var path = bindingContext.getPath(); 
var object = bindingContext.getModel().getProperty(path); 

如果你只想要你的顯示的某個屬性對象,你可以這樣做:

var property = bindingContext.getProperty("<nameOfProperty>"); 

whi ch在特定的上下文中返回一個對象的屬性。

更新:

您可以直接調用的BindingContext,它返回對象模型中的當前背景點的getObject()

var object = bindingContext.getObject(); 

更多信息,請參見documentation of Context

+0

我使用你的bindingContext/path/object調用並在那之後添加'console.log(object);'。在Chromes控制檯中,我可以看到對象的內容以及它具有的所有屬性。但是當我嘗試訪問像這樣的屬性時:'console.log(object.Id);'我得到'undefined'。任何想法爲什麼會發生? – michaelklopf

+0

@Deftoned:有時我在從模型中調試對象時遇到此問題。這可能會發生,因爲您持有指向對象的指針而不是對象副本。您可以嘗試在從bindingContext中取回對象後立即停止您的程序並讀取它,然後將其記錄到控制檯。 –

0

對於列表項「select」事件,從綁定上下文中獲取currect選定的項目記錄使用:。

evt.getSource().getSelectedItem().getBindingContext("yourModelName").getObject(); 
+0

實際上事件'Select'已被棄用,而是使用'selectionChange'事件。使用下面的代碼來處理事件 var oSelectedItem = oEvent.getParameter(「listItem」); oSelectedItem.getBindingContext(「yourModelName」)。getObject(); –

+0

而「listItem」僅適用於單個選擇。對於多個選擇,使用「listItems」,它返回所選項目的數組 –

相關問題