Mryqu's Notes


  • 首页

  • 搜索
close

[OpenUI5] JSView的createContent和Controller的onInit孰先孰后?

时间: 2014-11-14   |   分类: FrontEnd     |   阅读: 259 字 ~2分钟

首先在这个两个函数设置断点,很容易知道JSView的createContent先于Controller的onInit被调用。 [OpenUI5] JSView的createContent和Controller的onInit孰先孰后?[OpenUI5] JSView的createContent和Controller的onInit孰先孰后? 通过sap.ui.core.mvc.View源码片段可知,View的_initCompositeSupport函数中首先调用createAndConnectController函数创建Controller,之后调用的onControllerConnected函数会调用createContent函数,最后调用的fireAfterInit函数会触发Controller的onInit函数回调。

View.prototype._initCompositeSupport = function(mSettings) {

  // init View with constructor settings
  // (e.g. parse XML or identify default controller)

  // make user specific data available during view instantiation
  this.oViewData = mSettings.viewData;
  // remember the name of this View
  this.sViewName = mSettings.viewName;
  // remember the preprocessors
  this.mPreprocessors = mSettings.preprocessors || {};

  //check if there are custom properties configured for this view, 
  //and only if there are, create a settings preprocessor applying these
  if (sap.ui.core.CustomizingConfiguration && 
      sap.ui.core.CustomizingConfiguration.hasCustomProperties(
                     this.sViewName, this)) {
    var that = this;
    this._fnSettingsPreprocessor = function(mSettings) {
      var sId = this.getId();
      if (sap.ui.core.CustomizingConfiguration && sId) {
        if (that.isPrefixedId(sId)) {
          sId = sId.substring((that.getId() + "--").length);
        }
        var mCustomSettings = sap.ui.core.CustomizingConfiguration.
                  getCustomProperties(that.sViewName, sId, that);
        if (mCustomSettings) {
          // override original property initialization 
          // with customized property values
          mSettings = jQuery.extend(mSettings, mCustomSettings); 
        }
      }
    };
  }

  if (this.initViewSettings) {
    this.initViewSettings(mSettings);
  }

  createAndConnectController(this, mSettings);

  // the controller is connected now => notify the view implementations
  if (this.onControllerConnected) {
    this.onControllerConnected(this.oController);
  }
  
  this._preprocessViewContent();

  // notifies the listeners that the View is initialized
  this.fireAfterInit();

};

通过sap.ui.core.mvc.JSView源码片段可知,JSView的onControllerConnected函数中会调用createContent函数。

JSView.prototype.onControllerConnected = function(oController) {
  var that = this;
  var oPreprocessors = {};
  // when auto prefixing is enabled we add the prefix
  if (this.getAutoPrefixId()) {
    oPreprocessors.id = function(sId) {
      return that.createId(sId);
    };
  }
  oPreprocessors.settings = this._fnSettingsPreprocessor;
  // unset any preprocessors (e.g. from an enclosing JSON view)
  sap.ui.base.ManagedObject.runWithPreprocessors(function() {
    that.applySettings({ content : that.createContent(oController) });
  }, oPreprocessors);
};

通过sap.ui.core.mvc.Controller源码片段可知,Controller的connectToView方法就是将回调注册到View的生命周期事件监听器上。

Controller.prototype.connectToView = function(oView) {
  this.oView = oView;

  if (this.onInit) {
    oView.attachAfterInit(this.onInit, this);
  }
  if (this.onExit) {
    oView.attachBeforeExit(this.onExit, this);
  }
  if (this.onAfterRendering) {
    oView.attachAfterRendering(this.onAfterRendering, this);
  }
  if (this.onBeforeRendering) {
    oView.attachBeforeRendering(this.onBeforeRendering, this);
  }
  //oView.addDelegate(this);
};

标题:[OpenUI5] JSView的createContent和Controller的onInit孰先孰后?
作者:mryqu
声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 CN 许可协议。转载请注明出处!

#openui5# #createcontent# #oninit# #mvc# #javascript#
了解Google Closure Tools
[OpenUI5] 十分钟了解sap.ui.table.Table
  • 文章目录
  • 站点概览

Programmer & Architect

662 日志
27 分类
1472 标签
GitHub Twitter FB Page
© 2009 - 2023 Mryqu's Notes
Powered by - Hugo v0.120.4
Theme by - NexT
0%