class sap.f.routing.Targets

Control sample: sap.f.routing.Targets
Visiblity: public
UX Guidelines:
Implements:
Available since: N/A
Module: sap/f/routing/Targets
Application Component: CA-UI5-CTR

Provides a convenient way for placing views into the correct containers of your app.

The sap.f extension of Targets also handles the triggering of page navigation when the target control is an sap.f.FlexibleColumnLayout. Other controls are also allowed, but the extra parameters viewLevel, transition, and transitionParameters are ignored and it behaves as sap.ui.core.routing.Targets.

When a target is displayed, dialogs are being closed. To change this, use #getTargetHandler and sap.f.routing.TargetHandler#setCloseDialogs.


Constructor

Constructor for a new Targets class.

new sap.f.routing.Targets(oOptions)
Param Type Default Value Description
oOptions object
views sap.ui.core.routing.Views

The views instance will create the views of all the targets defined, so if 2 targets have the same viewName, the same instance of the view will be displayed.

config? object

This config allows all the values oOptions.targets.anyName allows, these will be the default values for properties used in the target.

For example, if you are only using xmlViews in your app you can specify viewType="XML" so you don't have to repeat this in every target.

If a target specifies viewType="JS", the JS will be stronger than the XML here is an example.


{
    config: {
        viewType : "XML"
    }
    targets : {
        xmlTarget : {
            ...
        },
        jsTarget : {
            viewType : "JS"
            ...
        }
    }
}

Then the effective config that will be used looks like this:

{
    xmlTarget : {
        // coming from the defaults
        viewType : "XML"
        ...
    },
    jsTarget : {
       // XML is overwritten by the "JS" of the targets property
       viewType : "JS"
      ...
    }
}

rootView? string

The ID of the rootView.

This should be the ID of the view that contains the control with the controlId since the control will be retrieved by calling the sap.ui.core.mvc.View#byId function of the rootView.

If you are using a component and add the routing.targets, do not set this parameter, since the component will set the rootView to the view created by the sap.ui.core.UIComponent#createContent function.

If you specify the "parent" property of a target, the control will not be searched in the root view but in the view created by the parent (see parent documentation).

async? boolean false

Whether the views which are created through this Targets are loaded asynchronously. This option can be set only when the Targets is used standalone without the involvement of a Router. Otherwise, the async option is inherited from the Router.

targets object

One or multiple targets in a map.

anyName object

A new target, the key severs as a name.

Example:


{
    targets: {
        welcome: {
            viewName: "Welcome",
            viewType: "XML",
            ....
            // Other target parameters
        },
        goodbye: {
            viewName: "Bye",
            viewType: "JS",
            ....
            // Other target parameters
        }
    }
}

This creates two targets named 'welcome' and 'goodbye'. You can display both of them or one of them using the #display function.

viewName string

The name of a view that will be created. To place the view into a Control use the controlAggregation and controlId. Views are only created once per viewName.


{
    targets: {
        // If display("masterWelcome") is called, the master view will be placed in the 'MasterPages' of a control with the id splitContainter
        masterWelcome: {
            viewName: "Welcome",
            controlId: "splitContainer",
            controlAggregation: "masterPages"
        },
        // If display("detailWelcome") is called after the masterWelcome, the view will be removed from the master pages and added to the detail pages, since the same instance is used. Also the controls inside of the view will have the same state.
        detailWelcome: {
            // same view here, that's why the same instance is used
            viewName: "Welcome",
            controlId: "splitContainer",
            controlAggregation: "detailPages"
        }
    }
}

If you want to have a second instance of the welcome view you can use the following:


// Some code you execute before you display the target named 'detailWelcome':
var oView = sap.ui.view(({ viewName : "Welcome", type : sap.ui.core.mvc.ViewType.XML});
oTargets.getViews().setView("WelcomeWithAlias", oView)

{
    targets: {
        // If display("masterWelcome") is called, the master viewName will be placed in the 'MasterPages' of a control with the id splitContainter
        masterWelcome: {
            viewName: "Welcome",
            controlId: "splitContainer",
            controlAggregation: "masterPages"
        },
        // If display("detailWelcome") is called after the masterWelcome, a second instance with an own controller instance will be added in the detail pages.
        detailWelcome: {
            // same viewName here, that's why the same instance is used
            viewName: "WelcomeWithAlias",
            controlId: "splitContainer",
            controlAggregation: "detailPages"
        }
    }
}

viewType? string oOptions.config.viewType

The type of the view that is going to be created. These are the supported types: sap.ui.core.mvc.ViewType. You always have to provide a viewType except if oOptions.config.viewType is set or when using sap.ui.core.routing.Views#setView.

viewPath? string

A prefix that is prepended in front of the viewName.

Example: viewName is set to "myView" and viewPath is set to "myApp" - the created viewName will be "myApp.myView".

viewId? string

The ID of the created view. This is is prefixed with the ID of the component set to the views instance provided in oOptions.views. For details, see sap.ui.core.routing.Views#getView.

targetParent? string

The ID of the parent of the controlId.

This should be the ID of the view that contains your controlId since the target control is retrieved by calling the sap.ui.core.mvc.View#byId function of the targetParent. By default, this is the view created by a component, so you do not have to provide this parameter. If you are using children, the view created by the parent of the child is taken. You only need to specify this, if you are not using a Targets instance created by a component and you should give the ID of root view of your app to this property.

controlId? string

The ID of the control where you want to place the view created by this target. The view of the target will be put into this container Control, using the controlAggregation property. You have to specify both properties or the target will not be able to place itself. An example for containers are sap.ui.ux3.Shell with the aggregation 'content' or a sap.m.NavContainer with the aggregation 'pages'.

controlAggregation? string

The name of an aggregation of the controlId, that contains views. For example, an sap.m.NavContainer has a pages aggregation and an sap.ui.ux3.Shell it has a content aggregation.

clearControlAggregation? boolean

Defines a boolean that can be passed to specify if the aggregation should be cleared - all items will be removed - before adding the View to it. When using a sap.ui.ux3.Shell this should be true. For a sap.m.NavContainer it should be false. When you use the sap.f.routing.Router the default will be false.

parent? string

A reference to another target, using the name of the target. If you display a target that has a parent, the parent will also be displayed. Also the control you specify with the controlId parameter, will be searched inside of the view of the parent not in the rootView, provided in the config. The control will be searched using the byId function of a view. When it is not found, the global id is checked.

The main usecase for the parent property is placing a view inside a smaller container of a view, which is also created by targets. This is useful for lazy loading views, only if the user really navigates to this part of your application.

Example: Our aim is to lazy load a tab of an IconTabBar (a control that displays a view initially and when a user clicks on it the view changes). It's a perfect candidate to lazy load something inside of it.

Example app structure:

We have a rootView that is returned by the createContent function of our UIComponent. This view contains an sap.m.App control with the ID 'myApp'


<View xmlns="sap.m">
    <App id="myApp"/>
</View>

an xml view called 'Detail'

<View xmlns="sap.m">
    <IconTabBar>
        <items>
            <IconTabFilter>
                <!-- content of our first tab -->
            <IconTabFilter>
            <IconTabFilter id="mySecondTab">
                <!-- nothing here, since we will lazy load this one with a target -->
            <IconTabFilter>
        </items>
    </IconTabBar>
</View>

and a view called 'SecondTabContent', this one contains our content we want to have lazy loaded. Now we need to create our Targets instance with a config matching our app:

    new Targets({
        //Creates our views except for root, we created this one before - when using a component you
        views: new Views(),
        config: {
            // all of our views have that type
            viewType: 'XML',
            // a reference to the app control in the rootView created by our UIComponent
            controlId: 'myApp',
            // An app has a pages aggregation where the views need to be put into
            controlAggregation: 'pages'
        },
        targets: {
            detail: {
                viewName: 'Detail'
            },
            secondTabContent: {
                // A reference to the detail target defined above
                parent: 'detail',
                // A reference to the second Tab container in the Detail view. Here the target does not look in the rootView, it looks in the Parent view (Detail).
                controlId: 'mySecondTab',
                // An IconTabFilter has an aggregation called content so we need to overwrite the pages set in the config as default.
                controlAggregation: 'content',
                // A view containing the content
                viewName: 'SecondTabContent'
            }
        }
    });

Now, if we call oTargets.display("secondTabContent") , 2 views will be created: Detail and SecondTabContent. The 'Detail' view will be put into the pages aggregation of the App. And afterwards the 'SecondTabContent' view will be put into the content Aggregation of the second IconTabFilter. So a parent will always be created before the target referencing it.

viewLevel? int

If you are having an application that has a logical order of views (eg: a create account process, first provide user data, then review and confirm them). You always want to show a backwards transition if a navigation from the confirm to the userData page takes place. Therefore you may use the viewLevel. The viewLevel has to be an integer. The user data page should have a lower number than the confirm page. These levels should represent the user process of your application and they do not have to match the container structure of your Targets. If the user navigates between views with the same viewLevel, a forward transition is taken. If you pass a direction into the display function, the viewLevel will be ignored.

Example:


    {
        targets: {
            startPage: {
                viewLevel: 0
                // more properties
            },
            userData: {
                viewLevel: 1
                // more properties
            },
            confirmRegistration: {
                viewLevel: 2
                // more properties
            },
            settings: {
                //no view level here
            }
        }
    }

Currently the 'userData' target is displayed.

  • If we navigate to 'startPage' the navContainer will show a backwards navigation, since the viewLevel is lower.
  • If we navigate to 'userData' the navContainer will show a forwards navigation, since the viewLevel is higher.
  • If we navigate to 'settings' the navContainer will show a forwards navigation, since the viewLevel is not defined and cannot be compared.

transition? string

Defines which transition of the sap.m.NavContainer is applied when navigating. If it is not defined, the NavContainer take its default transition.

transitionParameters? string

Defines the transitionParameters of the sap.m.NavContainer


Methods Overview

Method Description
sap.f.routing.Targets.extend

Creates a new subclass of class sap.f.routing.Targets with name sClassName and enriches it with the information contained in oClassInfo.

oClassInfo might contain the same kind of information as described in sap.ui.core.routing.Targets.extend.

sap.f.routing.Targets.getMetadata

Returns a metadata object for class sap.f.routing.Targets.

getTargetHandler

Returns the TargetHandler instance.

sap.f.routing.Targets.extend

Creates a new subclass of class sap.f.routing.Targets with name sClassName and enriches it with the information contained in oClassInfo.

oClassInfo might contain the same kind of information as described in sap.ui.core.routing.Targets.extend.

Param Type DefaultValue Description
sClassName string

Name of the class being created

oClassInfo object

Object literal with information about the class

FNMetaImpl function

Constructor function for the metadata object; if not given, it defaults to the metadata implementation used by this class

sap.f.routing.Targets.getMetadata

Returns a metadata object for class sap.f.routing.Targets.

getTargetHandler

Returns the TargetHandler instance.