cancel
Showing results for 
Search instead for 
Did you mean: 

How to access a value in model.js file from different views

former_member127164
Participant
0 Kudos

Hi experts,

i have a doubt that i have maintained a model.js file and having two views and two controllers.Now i have an object i.e., it contains set of arrays in model.js.Now i want to access this array in 2nd view's controller.js file.Can any one clarify my doubt?

Accepted Solutions (0)

Answers (3)

Answers (3)

former_member127164
Participant
0 Kudos

Thanks to all

saivellanki
Active Contributor
0 Kudos

Hi Suneel,

I'm not sure whether I understood the requirement properly or not. But, what I understood is

You're have a separate file in project structure named as model.js - Something like below:



var oModel = {

                 Labels: {

                           Header: {

                                     CodeLabel: "CompanyCode",

                                     ClientLabel: "Client",

                                     SLLabel: "StorageLocation",

                                     WCLabel: "WorkCenter",

                                     MATNRLabel: "Material",

                                     RQLabel: "RequiredQuantity"

                                }

                      }

                 }

You wanted to access the above file in second view.


If this was the requirement, you can include the above file in your html script like below -



<script src="Model.js" type="text/javascript"></script> // Make sure you are giving the correct path in src attribute

You can fetch this model in two different ways.


1. You can just do oModel.Labels.Header.CodeLabel , where you will get "CompanyCode" value.

2. As Sergio suggested, you can bind this model to your view and you can access the model by providing correct path. Consider you wanted to bind CompanyCode to your Label control, then you could do -



new sap.m.Label({text:"{/oModel/Labels/Header/CodeLabel}"});

                                             (or)

new sap.m.Label().bindProperty("text", "/oModel/Labels/Header/CodeLabel");


Regards,

Sai Vellanki.

SergioG_TX
Active Contributor
0 Kudos

you should assign the model to a view... the model can have one or more arrays / objects (read on named model) then from your view you could read the data from any of the endpoints in the model by using the correct path.

so if your model contains something like:

{

array1: SomeArray,

array2: anotherarray,

arrayN: anotherNArray

}

then you should be able to read ...   path:  /arrray1,  or /array2, or /arrayN

hope this helps