0

The view is declared this way:

<mvc:View controllerName="com.sap.my.app.controller.Edit"
  xmlns:l="sap.ui.layout"
  xmlns:mvc="sap.ui.core.mvc"
  xmlns:bpa="com.sap.my.app.control"
  xmlns:core="sap.ui.core"
  xmlns="sap.m"
  height="100%">
  <mvc:XMLView viewName="com.sap.my.app.view.AttachmentPreview"/>
</mvc:View>

How can I access the model defined in the Edit controller within the AttachmentPrevie controller?

The AttachmentPreview will be reused inside multiple other views. Not only in Edit view.

Charles
  • 11,367
  • 10
  • 77
  • 114
  • What do the controllers do that the sub-controller needs to access data from its parent controller? Have you considered [using `EventBus`](https://stackoverflow.com/a/46186706/5846045) instead? On the other hand, models are usually propagated to children automatically. Your sub-controller should be able to access the parent model (not in `onInit` though). Either way, we need more information to understand the problem better. – Boghyon Hoffmann Aug 21 '21 at 22:19

2 Answers2

0

Make you model global by declaring it in manifest.json file if possible or inside Component.js file

fabiopagoti
  • 1,467
  • 14
  • 31
  • Ok :/ Is there not another solution which can better scale accross ? What if I want to display the attachmentPreviewer multiple times on the same screen.... – Charles Aug 20 '21 at 12:17
  • 2
    I don’t see any impacts if you need to do that. Could you please elaborate your concern? Thanks – fabiopagoti Aug 20 '21 at 12:41
  • Let's say you have an email client and you can open two emails at once. Then you have two attachmentPreviewer but only one place in the global model... – Charles Aug 20 '21 at 13:43
  • 3
    I still don't see the problem. A model is basically just a storage for data (plain old javascript objects). Your `AttachmentPreview` can read this data. Open your `AttachmentPreview` twice just means reading the data twice. Don't see any scaling problems. – Marc Aug 20 '21 at 18:43
  • This feels like a global state management solution, but in many cases, the states (model) should not be global. Let's say I have a subdomain of Task, and its UI contains a TaskList page and a TaskDetail page. Why would I want to expose this Task model to other pages if these are the only two pages using it? Or let's say there're a few local UI-related properties or dynamic properties generated on the fly being set as JsonModel in the parent view, and I want to pass that down to its direct child views. These properties should not be global. Making everything global breaks encapsulation. – Xucong Aug 30 '22 at 02:40
0

You can simply refer to the parent view with the method getParent() in the child view e.g. this.getView().getParent().getModel(). Please do make sure that the view itself is already attached or else the getParent would return null.

Swadhin
  • 109
  • 6