如何在Init方法中调用函数?

时间:2017-02-24 16:09:59

标签: javascript sap sapui5

我是sapui5的新手,我想知道如何在我的Init方法中调用函数?

我目前的代码如下:

onInit: function() { 

        this.oInitialLoadFinishedDeferred = jQuery.Deferred();          
        var oEventBus = this.getEventBus();



        this.getView().byId("list").attachEventOnce("updateFinished", function() {
            this.oInitialLoadFinishedDeferred.resolve();
            oEventBus.publish("Master", "InitialLoadFinished", {
                oListItem: this.getView().byId("list").getItems()[0]
            });
        }, this);

        oEventBus.subscribe("Detail", "TabChanged", this.onDetailTabChanged, this);

        //on phones, we will not have to select anything in the list so we don't need to attach to events
        if (Device.system.phone) {
            return;
        }


        this.getRouter().attachRoutePatternMatched(this.onRouteMatched, this);

        oEventBus.subscribe("Detail", "Changed", this.onDetailChanged, this);
        oEventBus.subscribe("Detail", "NotFound", this.onNotFound, this);
        oEventBus.subscribe("Detail", "Cancelled", this.onDetailChangeCancelled, this);

        // this.handleSelectDialogPress();

    }   

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

我认为你几乎可以从init调用函数。

例如,如果要调用同一控制器内的某个功能,请使用'this'关键字(此处'this'指当前控制器) :

onInit: function() { 
   ...
   this.handleSelectDialogPress();
}

handleSelectDialogPress : function () {
  // some code
}

如果要调用某些Utils.js中的某个函数

,请参阅下面的代码

<强> ControllerName.controller.js

sap.ui.define([
   "sap/ui/core/mvc/Controller",
   "./MyUtils"
], function(Controller, MyUtils) {
   "use strict";

   return Controller.extend("your.controller.ControllerName", {
      myFunc2: function(input) {
         MyUtils.myFunc(input);
      }
   });
});

<强> MyUtils

sap.ui.define([], function() {
   "use strict";

   return {
      myFunc: function(input) {
         // some code
      }
   };
});