- Pattern: [“<url pattern>”, “<view or page id>”]
- Name: Tells router which view to load when given URL pattern is found in the URL
- View: Provide View or Page ID
- View Type: Mention what view type is for the view to be loaded
- targetControl: Specify shell ID which is the user control point or user interaction point
- targetAggregation: Content to be loaded when router navigates to another view
- clearTarget: Clears target when navigation triggers
- callback: Defines a function that places view defined in the router properties and sets that in the selected work item of the shell
worksetItemSelected: function(e) {
this.removeAllContent();
var selected = e.getParameter("key");
var oHashChanger = new sap.ui.core.routing.HashChanger();
oHashChanger.setHash(router.getURL(selected));
}
router.register("routerID");
router.initialize();
jQuery.sap.require("sap.ui.core.routing.Router");
jQuery.sap.require("sap.ui.core.routing.HashChanger");
We need reference to routing &
HashChanger both.<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'/>
<script src="resources/sap-ui-core.js"
id="sap-ui-bootstrap"
data-sap-ui-libs="sap.ui.commons,sap.ui.ux3"
data-sap-ui-theme="sap_bluecrystal">
</script>
<!-- add sap.ui.table,sap.ui.ux3 and/or other libraries to 'data-sap-ui-libs' if required -->
<script>
sap.ui.localResources("routerdemo");
jQuery.sap.require("sap.ui.core.routing.Router");
jQuery.sap.require("sap.ui.core.routing.HashChanger");
var oShell = sap.ui.ux3.Shell("shellId",{
appTitle: "routerdemo",
showLogoutButton: false,
showSearchTool: false,
worksetItems: [
new sap.ui.ux3.NavigationItem("View1Id",{
key: "View1",
text: "View1"
}),
new sap.ui.ux3.NavigationItem("View2Id", {
key: "View2",
text: "View2"
})
],
worksetItemSelected: function(e) {
this.removeAllContent();
var selected = e.getParameter("key");
var oHashChanger = new sap.ui.core.routing.HashChanger();
oHashChanger.setHash(router.getURL(selected));
},
content: [new sap.ui.view({
id: "view1Id",
viewName: "routerdemo.View1",
type: sap.ui.core.mvc.ViewType.JS
})]
});
oShell.placeAt('container');
var router = new sap.ui.core.routing.Router([
{
pattern: "",
name: "View1",
view: "routerdemo.View1",
viewType: sap.ui.core.mvc.ViewType.JS,
targetControl: "shellId",
targetAggregation: "content", //content/Page
clearTarget: true,
callback: function() {
oShell.setSelectedWorksetItem("View1Id");
}
},
{
pattern: ["custom", "View2"],
name: "View2",
view: "routerdemo.View2",
viewType: sap.ui.core.mvc.ViewType.JS,
targetControl: "shellId",
targetAggregation: "content", //content/Page
clearTarget: true,
callback: function() {
oShell.setSelectedWorksetItem("View2Id");
}
}
]);
router.register("routerId");
router.initialize();
</script>
</head>
<body class="sapUiBody" role="application">
<div id="container"></div>
</body>
</html>
Working ApplicationOn application initial load
On Navigation to View 2
Now, you can pass the URL with #/custom and application will directly load View 2 instead of loading view1 initially.
Index HTML
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'/>
<script src="resources/sap-ui-core.js"
id="sap-ui-bootstrap"
data-sap-ui-libs="sap.ui.commons,sap.ui.ux3"
data-sap-ui-theme="sap_bluecrystal">
</script>
<!-- add sap.ui.table,sap.ui.ux3 and/or other libraries to 'data-sap-ui-libs' if required -->
<script>
sap.ui.localResources("routerdemo");
jQuery.sap.require("sap.ui.core.routing.Router");
jQuery.sap.require("sap.ui.core.routing.HashChanger");
var oShell = sap.ui.ux3.Shell("shellId",{
appTitle: "routerdemo",
showLogoutButton: false,
showSearchTool: false,
worksetItems: [
new sap.ui.ux3.NavigationItem("View1Id",{
key: "View1",
text: "View1"
}),
new sap.ui.ux3.NavigationItem("View2Id", {
key: "View2",
text: "View2"
})
],
worksetItemSelected: function(e) {
this.removeAllContent();
var selected = e.getParameter("key");
var oHashChanger = new sap.ui.core.routing.HashChanger();
oHashChanger.setHash(router.getURL(selected));
},
content: [new sap.ui.view({
id: "view1Id",
viewName: "routerdemo.View1",
type: sap.ui.core.mvc.ViewType.JS
})]
});
oShell.placeAt('container');
var router = new sap.ui.core.routing.Router([
{
pattern: "",
name: "View1",
view: "routerdemo.View1",
viewType: sap.ui.core.mvc.ViewType.JS,
targetControl: "shellId",
targetAggregation: "content", //content/Page
clearTarget: true,
callback: function() {
oShell.setSelectedWorksetItem("View1Id");
}
},
{
pattern: ["custom", "View2"],
name: "View2",
view: "routerdemo.View2",
viewType: sap.ui.core.mvc.ViewType.JS,
targetControl: "shellId",
targetAggregation: "content", //content/Page
clearTarget: true,
callback: function() {
oShell.setSelectedWorksetItem("View2Id");
}
}
]);
router.register("routerId");
router.initialize();
</script>
</head>
<body class="sapUiBody" role="application">
<div id="container"></div>
</body>
</html>
Working ApplicationOn application initial load
On Navigation to View 2
Now, you can pass the URL with #/custom and application will directly load View 2 instead of loading view1 initially.
ConversionConversion EmoticonEmoticon