2014-02-20 69 views
1

在下面的代碼中,我手動檢查每個視圖是否有一些內容或它是否爲空。如果它有一些內容,那麼我正在摧毀它。我不想手動檢查每個視圖,我想要一個能夠動態選擇非空視圖的代碼,以便我可以銷燬該視圖並且不需要手動檢查。並且每次只有一個非空視圖。動態銷燬視圖

function reset_container() { 

     if (aasview != null) { 
      aasview.destroy(); 
     } 
     if (alewives != null) { 
      alewives.destroy(); 
     } 
     if (aa_amendview != null) { 
      aa_amendview.destroy(); 
     } 
     if (aa_dispatchview != null) { 
      aa_view.destroy(); 
     } 
     if (aa_postview != null) { 
      aa_postview.destroy(); 
     } 
     if (cc_dispatchview != null) { 
      cc_dispatchview.destroy(); 
     } 
     if (cc_postview != null) { 
      cc_postview.destroy(); 
     } 

     if (cm_salesview != null) { 
      cm_salesview.destroy(); 
     } 
     if(cc_view!=null){ 
      cc_view.destroy(); 
     } 
     if (cc_amendview != null) { 
      cc_amendview.destroy(); 
     } 
     if (quotationview != null) { 
      quotationview.destroy(); 
     } 
     if(truckview!=null){ 
      truckview.destroy(); 
     } 
     if (create_mnview != null) { 
      create_mnview.destroy();  
     } 
     if (create_stoview != null) { 
      create_stoview.destroy(); 
     } 
     if(vehicle_view != null){ 
      vehicle_view.destroy(); 
     } 
} 

回答

2

你是如何創建視圖?如果他們處於聚合中(例如在SplitApp主/細節頁面或應用頁面中),或者甚至只是在數組中,您可以簡單地遍歷它們以簡化您的代碼:

存儲在陣列中:

[view1, view2, view3].forEach(function(view) { 
    if (view != null) { //might not need this test depending on how you populate the array 
     view.destroy(); 
    } 
}) 

例如存儲在sap.m.SplitApp detailPages聚集:

this.oRoot.getDetailPages().forEach(function(view) { 
    view.destroy(); 
}); 

很難提供任何具體細節,沒有任何背景,以你的代碼。 ;-)我不知道爲什麼你甚至需要銷燬它們?

+0

他們不是在數組中。每個視圖的創建方式不同。因爲我在選擇特定菜單(標題)項目時將這些不同的視圖放在同一個容器上。 – Ash

+0

使用數組來存儲你的視圖,然後如我所示,Jasper_07在下面給出了很好的impl。 –

1

我必須做同樣的事情,並使用了註冊表

使用

jQuery.sap.require("my.viewRegistry"); 
// create view via the registry 
my.viewRegistry.createView('id', 'view.name', 'JS'); 

//access view 
var oView = my.viewRegistry.getView('id'); 

//delete view 
my.viewRegistry.deleteView('id'); 

//delete all views 
my.viewRegistry.setViews(); 

代碼

jQuery.sap.declare("my.viewRegistry"); 

my.viewRegistry = {}; 

(function() { 

    var _views = {}; // object that stores the view instances 

/** 
    * Creates a view for the given data 
    */ 
my.viewRegistry.createView = function(sId, sName, sType) { 
    // return view if already created 
    if (my.viewRegistry.getView(sId)) { 
     return my.viewRegistry.getView(sId); 
    } 
    //create view 
    var oView = sap.ui.view({ 
     id: sId, 
     viewName: sName, 
     type: sType 
    }); 
    //add view 
    _views[sId] = oView; 

    return oView; 
}; 

/* 
    * delete the view with the given id 
    */ 
my.viewRegistry.deleteView = function(sId) { 
    delete _views[sId]; 
}; 
/* 
    * get the view with the given id 
    */ 
my.viewRegistry.getView = function(sId) { 
    return _views[sId]; 
}; 

/* 
    * get all views 
    */ 
my.viewRegistry.getViews = function() { 
    return _views; 
}; 

/* 
    * set views 
    */ 
my.viewRegistry.setViews = function(oViews) { 
    _views = oViews; 
}; 

}());