jquery mobile popup doesn't wait for a click - jquery-mobile

Using jquery mobile 1.2, I have a popup menu (OK/Cancel) which should be answered before changing to a new page. But the page changes (and the popup disappears) before it is clicked:
if (rider.time.valueOf() > 0) {
$('#popupMsg').text("Rider has already finished; update the time?");
$('#alreadyFinished').popup("open");
}
alert("rf");
// other code.......
$.mobile.changePage("#finishLine");
// other code.......
I put in the 'alert' just to prove that the popup does actually appear - there it is, behind the alert, but the popup closes (and the page changes) as soon as the alert is clicked. Also tried removing the 'other code' but still the same problem.
Here's the html:
<div data-role="popup" id="alreadyFinished" class="ui-content">
<p id="popupMsg"></p>
<a data-role="button" data-theme="b" id="OKBtn">OK</a>
Cancel
</div>
Even without the buttons in the popup, the popup doesn't persist. So what's wrong?

A jQuery Mobile popup is part of a page. Changing the page will close any current popup, so you should not directly call it following the opening of a popup. The following code would open your popup and not close it.
if (rider.time.valueOf() > 0) {
$('#popupMsg').text("Rider has already finished; update the time?");
$('#alreadyFinished').popup("open");
} else {
alert("rf");
// other code.......
$.mobile.changePage("#finishLine");
// other code.......
}
If you want your popup to be modal, the simplest way is to bind the execution of the rest of your code to the closing of the popup. For instance, if you want the restOfCode function to be run after the popup is closed:
$('#alreadyFinished').popup("open");
$( "#alreadyFinished" ).on({
popupafterclose: function(event, ui) {restOfCode()}
});

Related

Link in popup doesn't works

I'm using jquery mobile to create a little mobile website.
I have a popup with a link, but this link works only if the popup is not taller than the phone screen. But it works on a computer...
The cross to close the popup is illuminated when I click on my link.
<div id="trouverUnMatch" data-role="page">
<div id="notification" data-role="popup" data-overlay-theme="b">
STUFF HERE
More
</div>
</div>
<div id="allNotifications" data-role="page">
</div>
Edit: It's not a problem with the bottom link, when I click on everywhere inside the popup it go to the popup top and illuminate the close button.
For example if you go to http://demos.jquerymobile.com/1.4.5/popup/ and click on a picture (Photo lightbox), you resize your browser to get only the half of it (in height), then if you click on the picture the close button will be illuminated.
I have replace the link by :
<span class="link" data-link="allNotifications">More</span>
$(document).ready(function() {
$(".link").on('tap', function () {
$.mobile.changePage( "#" + $(this).attr("data-link") );
});
});
It works with iOS but chrome on desktop has the problem now...

Dynamically close dialog

In my jQM - Backbone app I add a dialog programmatically if a certain condition is true, like this
$('body').append('<div data-role="dialog" id="interlink" data-theme="b" data-close-btn="none" data-url="insignificant"></div> ');
// remove dialog from DOM on pagehide
$("#interlink").on('pagehide', function () {
$(this).remove();
// remove this views popup-containers
$('#interlink-video-popup-popup').remove();
});
Beside other content in the dialog there is a button to open a popup widget to play a video clip and a close button to close the dialog. The code for closing the dialog looks like this:
backBtnHandler: function(e) {
e.preventDefault();
$('#interlink').dialog('close');
$(this).remove(); // all DOM listeners get removed as well by jQuery
}
This works all well if the video clip is watched in full length, the popup widget closes on ended and the user clicks the dialog close button to close it.
A requirement is when the video clip is playing and the user scans another NFC tag the video should stop, trigger an ended event and close the popup. This is also working, however the dialog should also close. Here is a simplified code snippet with a timeout to simulate a NFC scan:
INTERPRETOUR.interlinkVideoPlayer = $('#interlink-video-player')[0];
// bind onended event to close the popup
$(INTERPRETOUR.interlinkVideoPlayer).on('ended', function() {
$('#interlink-video-popup').popup('close');
INTERPRETOUR.interlinkVideoPlayer = 'undefined';
$('#interlink-back-btn').trigger('click');
});
// play video
INTERPRETOUR.interlinkVideoPlayer.src = 'http://mydomain.ca' + this.model.get('video')[0].url;
INTERPRETOUR.interlinkVideoPlayer.play();
setTimeout(function() {
$.publish('item', '2479');
}, 5000);
The issue is that $('#interlink-back-btn').trigger('click'); invokes the backBtnHandler but pagehide is never triggered and so the dialog doesn't close.
Any help to resolve this issue would be much appreciated.
Instead of invoking a button using .trigger('click'), bind closing when popupafterclose event triggers.
Demo 1 / Demo 2
Static Popup
$('#popupID').on('popupafterclose', function () {
$('#dialogID').dialog('close');
});
Dynamically generated Popup
$(document).on('popupafterclose', '#popupID', function () {
$('#dialogID').dialog('close');
});

jQuery Mobile event getting transferred from one page to other

Hi I have following HTML structure for my Android app with PhoneGap and JQM.
<body>
<div data-role="page" id="screen">
</div>
<div data-role="page" id="config" data-theme="a">
</div>
</body>
I am showing a time counter on #screen and some configuration to #config page. On touching anywhere on #screen stops counter and shows #config screen.
'#config' has a 'START' button on clicking which shows #screen again and starts counter.
To handle this I am using
$('#screen').bind('vmousedown', //code to show #config page);
$('#start_cycle').click(function(){
$.mobile.changePage('#screen');
});
My problem is that if I touches on #screen somewhere, where #start_cycle will appear, then on lifting my thumb back executes $('#start_cycle').click function.
It seems that my vmouseup event is performing click event on start button, without waiting for me to click it again.
How can I prevent this.
i have not tried this solution, but just by theory of how jQM works,
$('#screen').bind('vmousedown', function(e){
e.preventDefault();
//code to change the page to counter page.
return false;
})
The jQM says that, The v are followed by click events with a delay of 300ms on the same spot you touched. This might be the reason why your timer is getting started without you manually starting the timer. If the above didn't work, try attaching the touch events with native js methods.

Rendering of a button in jquery mobile

jQuery mobile button renders gibberish for filter button after clicking on pagination. Click event that shows map also stops working after changing the page.
Should I call refresh page?
Sandbox link
Markup is as following:
<div data-role="header" data-theme="b" id="rest-header">
<div class="ui-grid-b">
<div class="ui-block-a">Filter</div>
<div class="ui-block-b"><input type="search" name="search"value="" data-theme="d" /></div>
<div class="ui-block-c">Map</div>
</div>
</div><!-- /header -->
First I would suggest using jQuery 1.6.4 as jQM only supports this version for 1.0
Your problem is you're adding a back button when navigating to another page, this is causing an overlap of both the filter and back button.
Removing the data-add-back-btn="true" attribute should solve the issue
For map button you should use live event binding or delegation so that it works for dynamic content. Instead of
$( "#map-btn" ).bind( "click", function(event, ui) { ... });
try
$(document).on('click', '#map-btn', function() {
// ...
})
For filter button I can see the Back button under the Filter one, so you want to get rid of the Back button since you don't need it right there. You can remove data-add-back-btn="true" attribute.

jQuery UI - Dialog closes immediately when opened with onkeypress Enter/Space

I want to open a jQuery UI dialog when a user presses enter or space on a given element. It appears that the enter/space keys are being processed by the dialog, however, causing the default element (the cancel button) to be pressed. The dialog is closed almost as soon as it opens.
Here is a simplified demonstration:
<script type="text/javascript">
function Go() {
$("#dialog").text("Are you sure?").dialog({
modal: true,
buttons: {
Cancel: function() {
$("#dialog").dialog("destroy");
},
OK: function() {
$("#dialog").dialog("destroy");
}
}
}); // end .dialog
}
</script>
<input type="button" value="Test" onkeydown="Go()" />
<div title="Dialog" style="display: none;" id="dialog"></div>
If the button is given focus and then the user presses space the dialog opens and then immediately closes. If the user presses enter, the dialog closes so quickly it doesn't even flash (at least that was my experience with Firefox 3.5.3)
How do I prevent the dialog from processing the key from the onkeydown event which caused the dialog to open?
Try onkeyup if there is one. If it works, I'll give you the explanation(Busy).
Edit Explantion:
It's mainly because the onkeydown event does not get called once, it gets called continually while it's pressed down, and since computers opperate so fast(1 second is eternity to them) it get's called many times during one push down. The solution is not to call it when it's pushed down, but call it on the up part(when you let go).
Happens all the time in electronics with switches and logic gates etc. I forgot the technical terms.

Resources