From 0cdf431aa06d1513323f6143332b0af86e689d0d Mon Sep 17 00:00:00 2001 From: MattMo Date: Tue, 28 Dec 2021 22:33:36 -0800 Subject: [PATCH] Added IgniteTemplate navigate function to allow dynamic routing based on properties or quickly set up routing. --- ignite-html-router.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/ignite-html-router.js b/ignite-html-router.js index dd41d6d..97fbdae 100644 --- a/ignite-html-router.js +++ b/ignite-html-router.js @@ -1,6 +1,6 @@ import { IgniteElement } from "../ignite-html/ignite-element.js"; import { IgniteTemplate, slot, div, html } from "../ignite-html/ignite-template.js"; -import { IgniteCallback} from "../ignite-html/ignite-html.js"; +import { IgniteCallback, IgniteProperty} from "../ignite-html/ignite-html.js"; /** * Creates a route listener that runs when a route is switched. The listener will run on construct the first time and update the state. @@ -81,6 +81,23 @@ IgniteTemplate.prototype.route = function(routes, showCallback = null, hideCallb return this; }; +/** + * + * @param {String|IgniteProperty|Function} route The route to navigate to. + * @param {Any|IgniteProperty|Function} data The data to pass along with this navigate, this only applies when not refreshing, default is null. + * @param {Boolean|IgniteProperty|Function} refresh Whether or not to refresh the page, default is false. + * @returns {IgniteTemplate} This ignite template. + */ +IgniteTemplate.prototype.navigate = function(route, data = null, refresh = false) { + return this.onClick(() => { + Router.navigate( + (route instanceof IgniteProperty ? route.value : (route instanceof Function ? route() : route)), + (data instanceof IgniteProperty ? data.value : (data instanceof Function ? data() : data)), + (refresh instanceof IgniteProperty ? refresh.value : (refresh instanceof Function ? refresh() : refresh)) + ); + }); +} + class RouterLink extends IgniteElement { constructor() { super();