Skip to Content

Share model between controllers - global model vs eventbus

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    Feb 05, 2017 at 08:33 AM

    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);
        }
    }
    Add comment
    10|10000 characters needed characters exceeded

  • Feb 04, 2017 at 02:10 AM

    global model

    Add comment
    10|10000 characters needed characters exceeded