1

In sap.ui.model.odata.v2.ODataModel, there is a metadataLoaded method which I can use it like this:

this.getModel().metadataLoaded().then( function() {
    var sObjectPath = this.getModel().createKey("/", {
        ID :  sObjectId
    });
    this._bindView("/" + sObjectPath);
}.bind(this));

JSONModel doesn't seem to have a corresponding method since there is no service metadata concept in client-side models. So is there any other work around?

I tried attachRequestCompleted. It's not working as expected:

function bindview() {
    that._bindView(sObjectId);
}
this.getModel().attachRequestCompleted(bindview);
Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
Tina Chen
  • 2,010
  • 5
  • 41
  • 73

3 Answers3

0

As you rightly said, there is no metadata. But for Json model why do you want to wait for any event? You can directly bind the view.

krisho
  • 1,004
  • 7
  • 26
  • Sometimes I found that the API that get the JSON Model is pending when I call bindview in _onObjectMatched. (`this.getRouter().getRoute("object").attachPatternMatched(this._onObjectMatched, this);`) – Tina Chen May 05 '17 at 06:04
0

attachRequestCompleted only fires once when model is loaded, So I fix this problem in this way:

jsonModelLoaded: false,

_onObjectMatched : function (oEvent) {
    if(this.jsonModelLoaded) {
        this._bindView(sObjectId);
    }

    function bindview() {
        that.jsonModelLoaded = true;
        that._bindView(sObjectId);
    }
    this.getModel().attachRequestCompleted(bindview);
}

another not documented solution is using this.getModel().attachRequestCompleted().pSequentialImportCo‌​mpleted.then()

Related question here: Why metadataLoaded can be fired multiple times?

Tina Chen
  • 2,010
  • 5
  • 41
  • 73
0

Since 1.641, JSONModel returns a promise when loadData or dataLoaded is called. For example:

_onObjectMatched: function(oEvent) {
  const myJSONModel = this.getView().getModel();
  const sObjectId = /*...*/;
  myJSONModel.dataLoaded().then(() => this._bindView(sObjectId));
},

Unlike v2ODatamodel.metadataLoaded()2, the promise here actually rejects if the request fails.


1 Commit: bd4e2fc
2 The promise returned from metadataLoaded() never rejects even if loading the metadata document failed. However, since 1.79, apps can pass true as an argument to get a catch-able promise: metadataLoaded(true).

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170