title |
---|
Router |
Introduced with Q3 2014. If set to false
, the router instance will perform case sensitive match of the url against the defined routes.
If set to true, the router will use the history pushState API.
The history
pushState
API currently has limited support across current browsers.
Applicable if pushState
is used and the application is deployed to a path different than /
. If the application start page is hosted on http://foo.com/myapp/
, the root option should be set to /myapp/
.
<script>
var router = new kendo.Router();
var router = new kendo.Router({ pushState: true, root: "/myapp/" });
$(function() {
router.start();
router.route("bar", function() {
console.log("navigated to bar");
});
router.start();
});
</script>
Introduced in the 2014 Q1 Service Pack 1 release. If set to true
, the hash based navigation will parse and prefix the fragment value with !
,
which should be SEO friendly, and allows non-prefixed anchor links to work as expected.
<a href="#!bar">Go to bar</a>
<a href="#bar">I am a regular bar anchor tag</a>
<script>
var router = new kendo.Router({ hashBang: true });
$(function() {
router.start();
router.route("bar", function() {
console.log("navigated to bar");
});
});
</script>
Activates the router binding to the URL changes.
The
start
method should only be called once the routes have been registered. Otherwise the route for the current page/fragment will not be called.
<script>
var router = new kendo.Router();
$(function() {
router.start();
});
</script>
<script>
var router = new kendo.Router();
router.route("/items/:category/:id", function(category, id, params) {
console.log(category, "item with id", id, "was requested by", params.user);
});
$(function() {
router.start();
// ...
router.navigate("/items/books/59?user=John");
});
</script>
The route definition.
The callback to be executed when the route is matched.
Navigates to the given route.
The route to navigate to.
If set to true, the router callbacks will not be called.
<a id="link" href="#">Click me</a>
<script>
var router = new kendo.Router();
router.route("/items/:category/:id", function(category, id) {
console.log(category, "item with", id, " was requested");
});
$(function() {
router.start();
$("#link").click(function() {
router.navigate("/items/books/59");
return false;
});
});
</script>
Navigates to the given route, replacing the current view in the history stack (like window.history.replaceState
or location.replace
work).
The route to navigate to.
If set to true
, the router callbacks will not be called.
<a id="link1" href="#bar">Click me first</a>
<a id="link2" href="#">Click me second</a>
<a id="link3" href="#">Click me third</a>
<script>
var router = new kendo.Router();
$(function() {
router.start();
$("#link2").click(function() {
router.replace("baz");
return false;
});
$("#link3").click(function() {
history.back();
alert(location.href); // we will be back to the initial url at this point
return false;
});
});
</script>
Unbinds the router instance listeners from the URL fragment part changes.
Triggered when the user navigates back to the previous URL.
The current part of the URL
The fragment part of the previous URL
Calling the
preventDefault
method of the event object will stop the change and restore the previous URL.
Triggered when the fragment part of the URL changes.
The fragment part of the URL
The parsed query string parameters of the URL
Calling the
preventDefault
method of the event object will stop the change and restore the previous URL.
<a id="link" href="#">Click me</a>
<script>
var router = new kendo.Router();
router.route("/items/:category/:id", function(category, id, params) {
console.log(category, "item with", id, " was requested by", params.user);
});
router.bind("change", function(e) {
console.log("change event", e);
});
$(function() {
router.start();
$("#link").click(function() {
router.navigate("/items/books/59?user=John");
return false;
});
});
</script>
Triggered when the URL does not match any of the provided routes.
<script>
var router = new kendo.Router({ routeMissing: function(e) { console.log(e.url, e.params) } });
$(function() {
router.start();
router.navigate("/foo?bar=baz");
});
</script>
The fragment part of the URL
The parsed query string parameters of the URL
Calling the
preventDefault
method of the event object will stop the change and restore the previous URL.