How to retrieve view outside the controller - sapui5

If I use this.getView() inside the controller of a view I can retrieve it without problems.
How can I retrieve the view if I am outside the controller (e. g. in controller of another view)?
I try sap.ui.core.Core().byId("<name of view>") but it returns undefined.

You can instantiate another view using:
var view = sap.ui.jsview("<name of view>");
If you´re using different view types you can choose the necessary function from here.
To avoid multiple instantiation you could do something like this:
var view = sap.ui.getCore().byId("id");
if (view === undefined) {
view = sap.ui.jsview("id", "<name of view>");
}
See this for more details regarding view definition/instantiation and IDs.

When I create a view i set a id
var theView=sap.ui.xmlview("OperationDetail, "<name of view>");
then i retrieve it by id
var theView = sap.ui.core.Core().byId("OperationDetail");
var myPage=theView.byId("pageOperation");

varRequired = sap.ui.getCore().byId("<name of view>");
this keyword refers to only the particular controller where as sap.ui.getCore() refers to the project views.

Related

How can I set the owner component a view?

If I create a view and its controller in run time how can I connect it to a component while when I call getOwnerComponent it returns the component.
I can not found any setOwnerComponent for the controller or view.
You can do this by running the code that creates the new view inside a "runAsOwner" call:
var oView = oComponent.runAsOwner(function() {
return sap.ui.xmlview("myView", {
// view info
});
});
You can see more information about the runAsOwner function here. I have also made a small fiddle to demonstrate this: https://jsfiddle.net/93mx0yvt/21/.

How to set ID of a XML-View in a component environment?

I want to access a view's controller from a custom module with some utility functions. Basically you can do this that way:
var oController = sap.ui.getCore().byId("__xmlview1").getController();
The problem is that the above coding will not work in a real environment because __xmlview1is dynamically created by the framework. So I tried to find a possibility to set the ID of the view during instantiation. The problem is - I could not find one:
Trying to give the view an ID in the view.xml file does not work:
<mvc:View
controllerName="dividendgrowthtools.view.dividendcompare"
id="testID"
xmlns="sap.m"
...
Trying to set the ID in the router configuration of the component does not work either:
...
name: "Dividend Compare",
viewId: "test",
pattern: "Dividend-Compare",
target: "dividendcompare"
...
The problem is that I do not have direct control over the instantiation of the XML view - the component respectively the router does it.
So, is there a solution for that problem? Or at least a save way to get the view ID by providing the name of the view?
You should have a look at the SAPUI5 EventBus.
I am pretty sure, you want to let the controller to do something with the dividentcompare view. With the SAPUI5 Eventbus, you can publish actions from one controller to another witout braking MVC patterns.
In your dividendcompare.controller.js:
onInit : function() {
var oEventBus = sap.ui.getCore().getEventBus();
oEventBus.subscribe("MyChannel", "doStuff", this.handleDoStuff, this);
[...]
},
handleDoStuff : function (oEvent) {
var oView = this.getView();
[...]
}
Now, in your anothercontroller.controller.js:
onTriggerDividendStuff : function (oEvent){
var oEventBus = sap.ui.getCore().getEventBus();
oEventBus.publish("MyChannel", "doStuff", { [optional Params] });
}
You are now able to get the view from the dividentcontroller in every case from every other controller of your app. You dont access the view directly, this would brake MVC patterns, but can pass options to its controller and do the handling there.

How to get the reference to the another view control within some view?

ViewA created by "ViewA.view.js" is bound to a JSONModel. From within a view called ViewB, I would like to get the data in the JSONModel to which ViewA is bound.
How can I possibly get the reference to ViewA within "ViewB.view.js"?
You can do by using viewData property of a view.
Suppos you have a model : oModel in ViewA.
When you call a new view (ViewB) inside ViewA just do the followings.
var oViewB = sap.ui.view({
viewName: "myApp.ViewA",
type: sap.ui.core.mvc.ViewType.<type>,
viewData: oModel.getData()
});
inside ViewB's createContent:
createContent: function(oController) {
var oDataFromViewA = this.getViewData();
....
....
....
}
In general it's not a best practice to tightly couple views to share model data. A better approach is to use a global model which is available in all your views. You do this by calling sap.ui.getCore().setModel(modelInstance, modelName).

Using data from controller in view

I have a question about using parameters from the controller in the view.
This is my situation:
In my Controller I have :
foreach ($projects as $project) {
$projectTitle = $project->getName();
}
Now In my view I want to show every title from a project in a table.
How can I do this the right way? I also want to create a link with in my view like this :
"controller/action/projectid/1"
to edit the project.
Why not have an array called $titles and do an array_push inside the for loop like this:
array_push($titles,$project->getName())
And send the $titles directly to the view?

Identify which instance of the view is currently active in RCP?

I am creating an RCP application. I need to open multiple instances of the same view but with different data. I did it by setting secondary id for different instances of the same view. Specifically, my problem is as follows: Please take a look
I have a graph view called Views.GraphView. I opened different instances of it from a command called openGraphView to show different graphs. The command is as follows:
page.showView("Views.GraphView", Integer.toString(instanceNum++), IWorkbenchPage.VIEW_ACTIVATE);
Now, I have a command called TreeLayout on this Views.GraphView toolbar, which suppose to change the layout of the graph and it will operate on each instance of the view. But for this, I think, I need to identify which instance of the view is active. The TreeLayout command looks something like this:
IViewPart findView = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage(). findView( "Views.GraphView"); //I think in the findView I need to give the id of the view [but how can I put the secondary id?]
GraphView view = (GraphView) findView;
view.changeLayout(); //I wrote this method in the graph view to change the layout
//I just tried to print the secondary id, but it did not print anyting
System.out.println("From treelayout command:- " + view.getViewSite().getSecondaryId());
So how can I identify which instance of the view is currently active and to operate on it?
You can use IWorkBenchPage.findViewReference(String viewId, String viewId) , if it returns null, the view with viewId and viewId is not present in the current perspective.
If you have a ViewReference you can use ViewReference.getView(boolean restore) to get the view
so in your handler you get something like:
final IWorkbenchPage page = HandlerUtil.getActiveWorkbenchWindow(
event).getActivePage();
final int instanceNum = 8;//the number of instances that are created
for (int index = 0; index < instanceNum; index++) {
final IViewReference viewReference = page.findViewReference(
"Views.GraphView", Integer.toString(index));
if (viewReference != null) {
final IViewPart view = viewReference.getView(true);
if (view instanceof GraphView) {
final GraphView graphView = (GraphView) view;
graphView.changeLayout();
}
}
}
The view.getViewSite().getSecondaryId() method is the good one to identify a secondary view. This method only returns the Null string for the "primary" view: the one opened when user click Window -> Show View - Your View.
I don't understand why your view toolbar button has to operate on all the view instances. TO my eyes, you should have one button in each view instance toolbar operating only in its own view. If you really need to operate from one button to ALL the views I think you will have to keep the open views references yourself, because I think the workbench doesn't provide a findViews method returning a view array.
using the below code you should be able to get the current active view.
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart()
Active view and Editor name display on MessageDialogbox
public class Active_Workbench extends AbstractHandler{
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
// A part service tracks the creation and activation of parts within a workbench page.
IPartService service = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService();
MessageDialog.openInformation(HandlerUtil.getActiveWorkbenchWindow(
event).getShell(), "Current Workbench Window", service.getActivePart().getTitle().toString());
return null;
}
}
I hope this answer is useful.