Skip to Content
avatar image
Former Member

getView from odata read success handler

Hello,

sorry for my English :)

I've an app with component that load a view/controller

In the controller init and other methods I need to load odata from server (with odata read) but in the success handler I can't get the view to bind my new data to the model of a specific control.

I can do anythings going directly to the control by full ID (like '__xmlview0--companies', for example) with a sap.ui.getCore().byId('__xmlview0--companies') by this isn't the right way, I would like to get the control directly

If I use this.getView() in the read success function, "this" isn't the controller but the odataModel object.

If this can help:

root.view.xml:

<Input id="companies" showSuggestion="true" showValueHelp="true"/>

root.controller.js

sap.ui.define([
  "sap/ui/core/mvc/Controller",
  "sap/ui/core/format/NumberFormat"
], function (Controller, NumberFormat) {
  "use strict";
  return Controller.extend("AppName.controller.root", {
    onInit: function () {
      var myOdataModel = new sap.ui.model.odata.ODataModel('/mock/', true);
      myOdataModel.read('COMPANY', null, null, false, function (inutile, json) {
	var data = json.data.results;
	datas.companies = data;
	var model = new sap.ui.model.json.JSONModel();
	model.setData(datas.companies);
	var companiesInput = this.getView().byId('companies');
	companiesInput.setModel(model);
      });

Component.js:

sap.ui.define([
  "sap/ui/core/UIComponent",
  "sap/ui/Device"
], function (UIComponent, Device) {
  "use strict";
  return UIComponent.extend("AppName.Component", {
    metadata: {
      manifest: "json"
    },
    init: function () {
      UIComponent.prototype.init.apply(this, arguments);
    }
  });
});
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Best Answer
    Mar 20 at 05:28 PM
    sap.ui.define(["sap/ui/core/mvc/Controller","sap/ui/core/format/NumberFormat"],function(Controller, NumberFormat){"use strict";return Controller.extend("AppName.controller.root", {
        onInit:function(){
    
    var that=this;
          var myOdataModel =newsap.ui.model.odata.ODataModel('/mock/', true);
          myOdataModel.read('COMPANY',null,null, false,function(inutile, json){
    	var data= json.data.results;
    	datas.companies =data;
    	var model =newsap.ui.model.json.JSONModel();
    	model.setData(datas.companies);
    	var companiesInput = that.getView().byId('companies');
    	companiesInput.setModel(model);});
    Add comment
    10|10000 characters needed characters exceeded