cancel
Showing results for 
Search instead for 
Did you mean: 

getView from odata read success handler

Former Member
0 Kudos

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);
    }
  });
});

Accepted Solutions (1)

Accepted Solutions (1)

junwu
Active Contributor
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);});

Answers (0)