Hello,
I came here looking for what I guess is a solution to the same problem.
ons-navigator
supports pushPage(id, options)
.
id
can be the path and name of a separate (template/)file.
options
can be a JS object passed into that file.
But how can one specifiy the JS which uses the options in that file? I would use an event like init
from https://onsen.io/v2/api/js/ons-page.html#lifecycle (I just remembered the note from https://onsen.io/v2/guide/fundamentals.html#the-ons-object).
According to the attempts by @LittleOldLady , the JS has to be placed inside the ons-page in that template inline, it cannot be referenced as a separate file from there? I just tested this, referencing a file by src
indeed does not work.
So a solution is to have all code in one or more JS files referenced from index.html
and to use a one-liner like
ons.getScriptPage().onInit = myFunctionCoveringTheCodeForThePageLoadedHere;
inside the script in that template/file/page inline.
Or use the
document.addEventListener('init', function(event) {
var page = event.target;
if (page.matches('#id') { // id as in passed to pushPage()
myFunctionCoveringTheCodeForThePageLoadedHere(); // Set up page's dynamic content or behavior
}
});
way. (Note the listener is bound to document and acts on the bubbled up JS events.)
Any benefits/tradeoffs you know of you are willing to share?