javascript - WinJS Runtime Error -
javascript - WinJS Runtime Error -
i making feed reader in visual studio using javascript, , when run it, error:
0x800a138f - javascript runtime error: unable property 'addeventlistener' of undefined or null reference
and code affected is:
articlelistelement.addeventlistener("iteminvoked", iteminvoked);
articlelistelement refers variable references winjs.ui.listview.
thanks help!
if document.getelementbyid("articlelist") returning null, must attempting add together listener before dom loaded.
if started blank project template, in default.js you'll see code:
app.onactivated = 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. } else { // todo: application has been reactivated suspension. // restore application state here. } args.setpromise(winjs.ui.processall()); } };
until activated event called, dom won't ready , getelementbyid can homecoming null. (for total run-down of activation sequence, see chapter 3 of free ebook, programming windows store apps html, css, , javascript).
if you're using navigation or grid app templates, need place initialization code within page control's ready method (or processed method). navigation template, you'll see code in pages/home/home.js. (the same chapter of free ebook talks page controls.)
that's first step--if you're calling getelementbyid @ right time, should give div declare listview.
there's more story, however, because attach event handler winjs control, have create sure command has been instantiated. purpose of winjs.ui.processall in blank template code above (the same phone call made automatically part of loading page control).
winjs.ui.processall (or winjs.ui.process, works in individual element , children rather whole document), goes through dom looking data-win-control attributes. when finds one, parses data-win-options attribute (if provide one), , calls constructor identified in data-win-control parsed options object.
until happens, declare command div , not contain other command structure.
with page controls, again, within processed or ready methods controls should instantiated. in blank template, need attach completed handler winjs.ui.processall:
args.setpromise(winjs.ui.processall().then(function () { //controls have been instantiated, initialization here //note calling .then , not .done necessary because need homecoming promise //to args.setpromise. });
at point, then, document.getelementbyid("articlelist") should listview's host div.
the lastly step attach handler winjs command event, have add together listener command object not host div, through .wincontrol property of host element. code needs this:
articlelistelement.wincontrol.addeventlistener("iteminvoked", iteminvoked);
javascript winjs
Comments
Post a Comment