Aaron McMillin
Aaron McMillin

Reputation: 2667

jQuery Mobile - Auto open dialog

This should be an easy question, but there seems to be something strange going on. I'm using Django, and want to have a jQuery mobile dialog open with the current messages on any page view. After closing the dialog, the window should return the the page that was about to be viewed. So really the issue is how to open a dialog on page load, and then show the page.

This (any many variations on it) does not work:

{% if messages %}
<section class="dialog" id="messagesDialog" data-role="dialog" data-theme="a">
<header class="okay" data-role="header"><h1>This is a title</h1></header>
<div data-role="content">
    <ul>
        {% for message in messages %}
        <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
        {% endfor %}
    </ul>
</div> {# content #}
</section>

<script type="text/javascript">
$(document).one('pageinit', function(event) {
    $.mobile.changePage("#messagesDialog", 'pop', true, true);
});
</script>
{% endif %}

But this does:

{% if messages %}
<section class="dialog" id="messagesDialog" data-role="dialog" data-theme="a">
<header class="okay" data-role="header"><h1>This is a title</h1></header>
<div data-role="content">
    <ul>
        {% for message in messages %}
        <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
        {% endfor %}
    </ul>
</div> {# content #}
</section>

<script type="text/javascript">
$(document).one('pageinit', function(event) {
    $.mobile.changePage("WhatHeck?!?!"); // This will return a 500 error from the server
    $.mobile.changePage("#messagesDialog", 'pop', true, true);
});
</script>
{% endif %}

So, is this a known issue, or something I'm missing? The only way I can get the page to pop the dialog and then show the correct page after closing it, is by calling $.mobile.changePage() to cause an error before changing to the dialog.

===Added===

No Error, no dialog, correct page:

$("section@[data-role=page]").one('pagechange', function(event) {
    //$.mobile.changePage("WhatHeck?!?!");
    $.mobile.changePage("#messagesDialog",{
        transition : 'pop',
        reverse    : false,
        changeHash : true
    });
});

No Error, opens dialog, wrong page:

$(document).one('pagechange', function(event) {
    //$.mobile.changePage("WhatHeck?!?!");
    $.mobile.changePage("#messagesDialog",{
        transition : 'pop',
        reverse    : false,
        changeHash : true
    });
});

500 error from server requesting /WhatHeck?!?! (expected), opens dialog, correct page:

$(document).one('pagechange', function(event) {
    $.mobile.changePage("WhatHeck?!?!");
    $.mobile.changePage("#messagesDialog",{
        transition : 'pop',
        reverse    : false,
        changeHash : true
    });
});

No error, opens dialog, wrong page:

$(document).one('pagechange', function(event) {
    $.mobile.changePage($("section@[data-role=page]"));
    $.mobile.changePage("#messagesDialog",{
        transition : 'pop',
        reverse    : false,
        changeHash : true
    });
});

Error: "Uncaught TypeError: Cannot call method '_trigger' of undefined", No dialog, correct page:

$(document).one('pagechange', function(event) {
    $.mobile.changePage($("section@[data-role=page]",{
        changeHash : true
    }));

    $.mobile.changePage("#messagesDialog",{
        transition : 'pop',
        reverse    : false,
        changeHash : true
    });
});

No Error, opens dialog, wrong page:

$(document).one('pagechange', function(event) {
    $.mobile.changePage("#Home");
    $.mobile.changePage("#messagesDialog",{
        transition : 'pop',
        reverse    : false,
        changeHash : true
    });
});

Actually works!

$(document).one('pagechange', function(event) {
    $.mobile.changePage("#Home", {
        allowSamePageTransition : true
    });

    $.mobile.changePage("#messagesDialog",{
        transition : 'pop',
        reverse    : false,
        changeHash : true
    });
});

Also works (For any page):

$(document).one('pagechange', function(event) {
    $.mobile.changePage($("section@[data-role=page]"), {
        allowSamePageTransition : true
    });

    $.mobile.changePage("#messagesDialog",{
        transition : 'pop',
        reverse    : false,
        changeHash : true
    });
});

So the solution to my problem, is the answer to this question: What is happening in the jquery mobile state when the error page fails to load?

So it seems like, when calling changePage from inside the pagechange event handler, the history had not yet been updated. Still doesn't make sense to me that I need to force it to reload the current page, then open the dialog. That's at least a better solution then expecting an error from the sever.

Upvotes: 1

Views: 5137

Answers (1)

Jasper
Jasper

Reputation: 75993

It looks like you are using syntax for an old version of jQuery Mobile (which is why you're getting an error). If you are using jQuery Mobile later than 1.0a4.1 then you should take a look at this page: http://jquerymobile.com/demos/1.1.0/docs/api/methods.html

Using the syntax of the documentation link above, here is what your code should look like:

$(document).one('pageinit', function(event) {
    $.mobile.changePage($("#messagesDialog"), {
        transition : 'pop',
        reverse    : true,
        changeHash : true
    });
});

If you are using jQuery Mobile 1.0a4.1 then upgrade, it's a year old at this point and isn't particularly stable or feature rich (compared to the latest 1.1.0 build).

Upvotes: 1

Related Questions