xml - Windows 8 Javascript app activation/launch deferral -
so in windows 8 winjs app i'm coding, trying loading of xml file take place in app startup sequence, while splash screen still showing, xmldoc element needed when home page loads, , loading of home page fail without it.
this initiation sequence in default.js:
(function () { "use strict"; var activation = windows.applicationmodel.activation; var app = winjs.application; var nav = winjs.navigation; var sched = winjs.utilities.scheduler; var ui = winjs.ui; app.addeventlistener("activated", function (args) { if (args.detail.kind === activation.activationkind.launch) { if (args.detail.previousexecutionstate !== activation.applicationexecutionstate.terminated) { // todo: application has been newly launched. initialize // application here. console.log("newly launched!"); var localsettings = windows.storage.applicationdata.current.localsettings; winjs.namespace.define("myglobals", { localsettings: localsettings }); // app run type check , sequence (first run / not first run): if (myglobals.localsettings.values['firstruncompleted']) { console.log("not first run!"); // cache version check. if app has been updated, initiate newly added cache values here: } else { console.log("first run!") myglobals.localsettings.values['firstruncompleted'] = true; }; //loadxml(); have tried many things this. doesn't work. } else { // todo: application has been reactivated suspension. // restore application state here. var currentvolume = app.sessionstate.currentvolume; if (currentvolume) { console.log("restore suspension"); console.log(currentvolume); }; } nav.history = app.sessionstate.history || {}; nav.history.current.initialplaceholder = true; // optimize load of application , while splash screen shown, execute high priority scheduled work. ui.disableanimations(); var p = ui.processall().then(function () { return nav.navigate(nav.location || application.navigator.home, nav.state); }).then(function () { return sched.requestdrain(sched.priority.abovenormal + 1); }).then(function () { ui.enableanimations(); }); args.setpromise(p); args.setpromise(winjs.ui.processall().then(function completed() { loadsavedcolour(); // populate settings pane , tie commands settings flyouts. winjs.application.onsettings = function (e) { e.detail.applicationcommands = { "helpdiv": { href: "html/help.html", title: winjs.resources.getstring("settings_help").value }, "aboutdiv": { href: "html/about.html", title: winjs.resources.getstring("settings_about").value }, "settingsdiv": { href: "html/settings.html", title: winjs.resources.getstring("settings_settings").value }, }; winjs.ui.settingsflyout.populatesettings(e); }
as can see have commented line of "loadxml()", need loadxml() function take place. here loadxml() function:
function loadxml() { windows.applicationmodel.package.current.installedlocation.getfolderasync("foldername").then(function (externaldtdfolder) { externaldtdfolder.getfileasync(myglobals.localsettings.values['currentbook']).done(function (file) { windows.data.xml.dom.xmldocument.loadfromfileasync(file).then(function (doc) { winjs.namespace.define("myglobals", { xmldoc: doc, }); }) }) }); };
(loadxml working function , works in other scenarios)
however, issue before loadxml function finishes, app splash screen goes away, , next home.html home page loads, starts accompanying home.js, has function requires myglobals.xmldoc object loadxml should have made. crashes app, myglobals.xmldoc undefined/null. used have app working running loadxml in home.js home.html page directly, in scenario xml document reloaded every time navigation made page, wasting time , resources. such, i'm trying move xmldocument loading app startup/initialization. lot!
loadxml
has async functionality , need handle that.
you shouldn't expect loadfromfileasync
(or of other async functions) have completed before returns caller. if code doesn't wait, you'll find myglobals.xmldoc
value won't set when need it.
i've renamed below more accurate behavior. big change returns promise
can used caller wait xml doc loaded. promise
used other promise
s wait on multiple conditions if you'd (or in case, other async work).
function loadxmlasync() { return new winjs.promise(function (complete, error, progress) { var localsettings = myglobals.localsettings.values; var installedlocation = windows.applicationmodel.package.current.installedlocation; installedlocation.getfolderasync("foldername").then(function (externaldtdfolder) { externaldtdfolder.getfileasync(values['currentbook']).done(function (file) { windows.data.xml.dom.xmldocument.loadfromfileasync(file).then(function (doc) { complete(doc); }); }); }); }); };
then, in use:
loadxmlasync().then(function(doc) { winjs.namespace.define("myglobals", { xmldoc: doc, }); // , other code should wait until has completed });
the code above not handle errors.
Comments
Post a Comment