cancel
Showing results for 
Search instead for 
Did you mean: 

How to reuse the Odata Model to populate the second view

rakesh2901
Participant
0 Kudos

Hi Experts,

I am creating an app with 2 views .I have created a named odata Model in Manifest.json with Name: matDescModel and as per understanding this model can be accessed from any view (am I correct).

Now on a button click I am poulating this model with backend data as below :

	var matnr = this.getView().byId("__inp0").getValue();
			var path = "matDescModel>/GetMatDescSet('" + matnr + "')";
			this.getView().bindElement(path);

and I am able to see data in View with binding as below:

	<Text id="__text0" text = "{matDescModel>Maktx}" class = "textStyle"/>

its working great,

Now on View 2 I am doing the same kind of binding :

	<Text text="{matDescModel>Matnr}" id="__idmatnr" class="textStyle"/>

with code below in view 2 controller in onInit() :

	var oModel = this.getOwnerComponent().getModel("matDescModel");
		    this.getView().setModel(oModel);

I am able to see the oData in the debugger code when I am seeing the oModel for view2, see the screen shot

odata.jpg

But on view 2 I am not seeing the data on screen,

is it a binding issue?? or framework only allows binding once?

Do i need to do binding manually from controller or how it can be achieved??

Regards

Rakesh

Accepted Solutions (0)

Answers (2)

Answers (2)

former_member227918
Active Contributor
0 Kudos

you cant restrict the backend call for second view, you can try one of the way as below,

1) in the change event of binding, you can get the data set it to global json model (define in component.js),

this.getView().bindElement({
path: path,
events: {
  change: this._onBindingChange.bind(this)
  }
});
and 
_onBindingChange: function(){
  var data = this.getView().getBindingContext().getObject();
  //set this data to json model and use in second view
  // OR for quick way you can carry in global variable i.e. $.sap.view1Data = data, and you can access $.sap.view1Data in second view
}

2) do a odata read call and prepare a json model and use in both view

3) you can pass the value to second view using router in the pattern

rakesh2901
Participant
0 Kudos

Hello Akhilesh,

Thanks for your response, from your solution above I can draw few conclusion:

1) An Odata Model cannot be binded to 2 different UI element in 2 different view. Am i correct?

2) Only way to achieve this is by JSON model means if I want to reuse data in multiple view it has to be done through JSON model not Odata Model

I dont want to use global variable.

from your solution I got this question : When does the Binding Change Event Called??

Please reply.

Rakesh

former_member227918
Active Contributor
0 Kudos

1) you are wrong, your odata model is based on odata service not an entity you are using for view1, your view1 bind element means odatamodel+entity which will go to the backend and pull the data, scope is offcourse current view only unless you take it into some other model to reuse. still any doubt ? i would suggest look at odatamodel tutorials.

2) once your entity data loaded you can get data using getProperty(), and yes if you want to reduce backend call json model is only way.

why not global model, and when you do element binding you can define change event into it and will trigger everytime.

Fabrice
Active Participant
0 Kudos

Hi,

I think it is because you set the model to the second view but you did not bind the view.

You have to bind the view with the same path as the first view

var path ="matDescModel>/GetMatDescSet('" + matnr + "')";
this.getView().bindElement(path);

Regards

Fabrice

rakesh2901
Participant
0 Kudos

Hey Fabrice,

Thanks for your reply.

Yes you are right, if I am doing the binding as you suggested it is working fine, I have seen that. But if I am doing so the oData is being called again from backend and I am wondering if data is already there why should I call the odata again from backend, its not practical.

How can I call odata only once and bind it to multiple views, this is what I want or is it not possible at all??

Regards

Rakesh