Hi all
I’ve been rooting around in the framework for an answer to this myself, as my app generates almost all of its content based upon dynamic configuration files and database data, and I have a couple of solutions, neither is particularly robust as one uses an (from an admittedly non-exhaustive search) undocumented router.navigate()
parameter and one uses an undocumented function of router.load()
the two solutions are below, use whichever you feel is more maintainable for yourself.
The main issue is how do we get runtime dynamically generated content into a route so that we can load it. The content
and el
properties are fine for defining your pages ahead of time, but what if that’s not possible? We need a way to feed content into the page load process itself.
Method the First
Firstly you need to define a route in your routes.js
thats not doing anything in particular. In the example I’ve used /dynamicLoad/
routes = [
{
path: '/',
url: './index.html',
},
{
name: "dynamicLoad",
path: "/dynamicLoad/"
},
{
path: '(.*)',
url: './pages/404.html',
},
];
Next in your navigate function, specify the parameter of route
as an object with the properties of content
or el
. In the code this utils.extend
's your original route with the new properties on the fly.
app.views.main.router.navigate({url:"/dynamicLoad/", route:{content:"<div class='page'>woof</div>"}});
Also works for el:
let testElement = document.createElement("div");
testElement.classList.add("page");
testElement.innerHTML = "woof";
app.views.main.router.navigate({url:"/dynamicLoad/", route:{el:testElement}});
Method the Second
You can hook directly into the router.load
function. You don’t need to create any routes here, and the advantage is your URL can be whatever you want it to be (it doesn’t need to match anything). Disadvantages are I’m not sure what very important logic
you’re missing from router.navigate as I’m new to the framework. So proceed with caution.
Note that you must include the url parameter, even if you don’t actually need to reference it for anything.
let testElement = document.createElement("div");
testElement.classList.add("page");
testElement.innerHTML = "woof";
app.views.main.router.load({url:"anythingYouWant", el:testElement});
This method should also work replacing el
with content
and passing it an html string
Happy Apping.