动态破坏视图

时间:2014-02-20 05:52:10

标签: javascript jquery sapui5

在下面的代码中,我手动检查每个视图是否有某些内容或是否为空。如果它有一些内容,那么我正在摧毁它。我不想手动检查每个视图,我想要一个动态选择非空视图的代码,以便我可以销毁该视图而不需要手动检查。并且一次只有一个非空视图。

    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 个答案:

答案 0 :(得分:2)

您是如何创建视图的?如果它们在聚合中(例如在SplitApp主/详细信息页面或应用程序页面中),或者甚至只是在数组中,则可以简单地循环它们以简化代码:

e.g。存储在数组中:

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

e.g。存储在sap.m.SplitApp detailPages聚合:

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

很难在没有任何上下文的情况下提供任何具体细节。 ;-)我不确定为什么你甚至需要销毁它们?

答案 1 :(得分: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;
 };

}());