WinJS 2.0 Page Seems to Get Disposed Unexpectedly RRS feed

  • Question

  • I have a page (WinJS.UI.Pages) in my app. In the JavaScript code I often use "this.element", which seems to be the root HTML element for the page.

    For example, the following line gets me the ListView control in my page:


    The problem is that sometimes "this.element" equals null for no apparent reason. When this happens, "this._disposed" equals true, so I suspect that the page gets disposed for some reason.
    Some cases are more clear than others: when navigating away from a page, the page's promises can still run. In this case, the page is disposed and "this.element" is null and the code is still running. But in other cases I can not see why this happens. I have to figure it out because my code then stops working.

    I see that in base.js (of WinJS), there's a _mixin member in the WinJS.UI.Pages which has a dispose function that sets this._disposed to true and this.element to null. At first I thought that it might be called, but setting a breakpoint there showed me that while it is called in the case of navigating away from a page, it is not breaking on the other cases. I don't know what else can set this._disposed to true and this.element to null, and why.

    It's hard to debug this because it seems to happen rarely and randomly and I can't find a solid scenario to recreate this.

    Any help will be appreciated :)

    Sunday, September 22, 2013 3:07 PM

All replies

  • Ok, I made some progress and can add some more specific details.

    My page has a custom control, which is implemented as a page in itself, and firing a custom event. Here's what the custom control is doing:

    WinJS.Application.queueEvent({ type: "userResponded", userResponse: shouldRefresh });

    The containing page listens to that event and handles it:

    WinJS.Application.addEventListener("userResponded", function (args) { //at this point (in the first time)
    //this.element == null
    //this._disposed == true
    //and in the second time:
    //this.element = {...}
    //this._disposed = false });

    For some reason, this event handler is called twice, one immediately after another. In the first time the page seems to be disposed and in the second time it magically comes back to life.
    Can anyone explain this behavior?

    In order to be able to keep running without getting the exception, the following workaround seems to work (just adding an if to get passed the first time):

    WinJS.Application.addEventListener("userResponded", function (args) {
        if (this.element != null) {
            //do something with this.element

    Sunday, September 22, 2013 5:03 PM