onInit 中的 OData V2 SetProperty

OData V2 SetProperty in onInit

我需要在 onInit 期间将 属性 设置为 DataSet,以更改视图中某些控件的可见性。我可以通过在控制器中动态设置可见性来解决这个问题。但是我想在视图中使用表达式绑定和可见的 属性 来设置可见性。

我在 OnStartSetVisibilites 函数中遇到错误。 self.getView().getBindingContext() returns 未定义。没有 sPath,我无法使用 setProperty。如果没有 setProperty,我的视图控件就不知道可见值。

如何解决这个问题?

查看:

<uxap:ObjectPageSubSection visible="{= ${Responsible} === '1'}" id="leader">
                    
</uxap:ObjectPageSubSection>

视图控制器中的 OnInit:

    onInit: function () {
        var startupParameters = this.getOwnerComponent().getComponentData().startupParameters;

        var sWorkitem = startupParameters.TASK[0];

        this.setModel(this.getOwnerComponent().getModel());
        this.getModel().metadataLoaded().then(function () {
            var sObjectPath = this.getModel().createKey("DataSet", {  
                Workitem: sWorkitem 
            });
            this.getView().bindElement({
                path: "/" + sObjectPath
            });
        }.bind(this));

        var self = this;
        var model = this.getOwnerComponent().getModel();

        this.getModel().read("/CharSet", {
            success: function (response) {
                $.sap.Chars = response.results;

                self.onStartSetVisibilities(model, self);

            }
        });

        // self.getView().attachAfterRendering(function () {
        //  self.onStartSetVisibilities(model, self);
        // });
        
    }

OnStartSetVisibilities:

    onStartSetVisibilities: function (model, self) {
        var char = model.getProperty(„GeneralData/Char");

        if (char !== "" || char !== null) {
          model.setProperty(self.getView().getBindingContext().sPath + "/Responsible", 
          this.getResponsibleForChar(char));
        }
    }

我整理了一些可能会解决您的问题的代码(未经测试,因此可能包含语法错误!)。

我介绍了 Promises which simplifies the asynchronous behavior of JS. I also replaced some of the inner functions with Arrow functions 的概念,因此您不必处理 thatself。箭头函数基本上使用它们在其中定义的范围的 this

您的应用现在应该有一个正确的流程。

  1. 首先绑定视图。
  2. 绑定视图后,您阅读 CharSet
  3. 读取数据后设置可见性
onInit: function () {
    const startupParameters = this.getOwnerComponent().getComponentData().startupParameters;
    const sWorkitem = startupParameters.TASK[0];
    
    this._bindView(sWorkitem)
        .then(() => this._readCharSet())
        .then(() => this._setVisibilities())
},
    
_bindView: function (sWorkitem) {
    return new Promise((resolve) => {
        const oModel = this.getOwnerComponent().getModel();
        oModel.metadataLoaded().then(() => {
            const sPath = "/" + oModel.createKey("DataSet", {  
                Workitem: sWorkitem 
            });
            this.getView().bindElement({
                path: sPath,
                events: {
                    change: resolve,
                    dataReceived: resolve
                }
            });
        });
    });
},
    
_readCharSet: function () {
    return new Promise((resolve) => {
        const oModel = this.getOwnerComponent().getModel();
        oModel.read("/CharSet", {
            success: resolve
        });
    });
},
    
_setVisibilities: function () {
    const oModel = this.getOwnerComponent().getModel();
    const sChar = oModel.getProperty("GeneralData/Char");

    if (sChar) {
        // ...
    }
}