cancel
Showing results for 
Search instead for 
Did you mean: 

Share model between controllers - global model vs eventbus

former_member277448
Participant
0 Kudos

hello,

i have a controller/view that displays a form for the user to make selections.

the selections are stored in a json model.

i need to pass the selections model to another view/controller where other stuff is done.

i could create a global json model and access it from both views or pass the model from one view to the other.

any suggestions as to which way is better?

cheers

pas

Accepted Solutions (1)

Accepted Solutions (1)

boghyon
Product and Topic Expert
Product and Topic Expert
0 Kudos

Depending on your business case, there are several approaches when it comes to setting models. Generally, You'd set your model on your ...

  • Component if your model should be available across all of your views that are descendants of that component. Models defined in the app descriptor (manifest.json) are set automatically on the component and will stay there until the end of the component's lifecycle.
  • View if the data is not needed elsewhere. The model is available during the view's lifecycle only.
  • Core if your app is not Component-based (e.g. for a small demo on jsbin) since core models won't be automatically propagated to the Component as described here.


In your case, if you have only two views that need the selection data, you could set the model to the first view and then pass it on to the second view on navigation. Something like this:

<App navigate=".onNavigate" />
onNavigate: function(event) { // minimal example
    const targetView = event.getParameter("to");
    if (targetView.byId("/*idOfThatPageIWant*/")) {
        const sourceView = event.getParameter("from");
        const modelName = "/*thatModelName*/";
        targetView.setModel(sourceView.getModel(modelName), modelName);
    }
}
former_member277448
Participant
0 Kudos

Thank you Boghyon Hoffman for your reply.

Answers (1)

Answers (1)

junwu
Active Contributor
0 Kudos

global model

Former Member
0 Kudos

Hi Jun Wu,

Any reason why global model is preferred over the other? I am just trying to understand the rationale behind your thoughts.

Lets say if we have bunch of pages in our app, with global model approach, we tend to provide access to unwanted views (which in a way is polluting the global space)

Your thoughts could clarify better.

Thanks,

Murali L

junwu
Active Contributor
0 Kudos

global model within the component

former_member277448
Participant
0 Kudos

Hi Jun Wu,

are you saying the model should be created in Component.js so that the views that 'hang off' the component can access the model.

cheers

pas.

junwu
Active Contributor
0 Kudos

yes..............just like the context in the component controller(web dynpro)

former_member277448
Participant
0 Kudos

Thank you Jun Wu.