I've converted a multi-page app intro single pages and now the swipes keep using the first page's targets.
On my first page I have:
$(document).on('pageshow', '#firstPage', function() {
$(document).on("swipeleft", function(event) {
$("body").pagecontainer("change", "secondPage.html", {
transition: "fade"
});
event.stopImmediatePropagation();
});
$(document).on("swiperight", function(event) {
$("body").pagecontainer("change", "lastPage.html", {
transition: "fade"
});
event.stopImmediatePropagation();
});
});
With HTML:
<div data-role="page" id="firstPage" data-dom-cache="false">
<!--CONTENT -->
<!-- PAGE JS -->
</div>
It works on the firstpage, but when I get to the second page it uses the first targets - secondPage.html and lastPage.html
my secondPage has this:
$(document).on('pageshow', '#secondPage', function() {
console.log('showing tags');
$(document).on("swipeleft", function(event) {
$("body").pagecontainer("change", "thirdPage.html", {
transition: "fade"
});
event.stopImmediatePropagation();
});
$(document).on("swiperight", function(event) {
$("body").pagecontainer("change", "firstPage.html", {
transition: "fade"
});
event.stopImmediatePropagation();
});
});
and HTML:
<div data-role="page" id="secondPage" data-dom-cache="false">
<!--CONTENT -->
<!-- PAGE JS -->
</div>
Etc etc...
I must add that my js code on each page is before the closing </div> of the page...so is being loaded by AJAX.
I did have this working on multi-page template, but the app has become quite big and for simplicity I decided to split it into separate pages.
Related
I am trying to populate a content into an bootstrap popover using angular and ajax. Although the data have been loaded correctly (as i can see in console.log) the result don't appear in the popover's content. I think the angular's loading is not ready yet when the bootstrap popover is loaded. Thus, how can i populate this popover immediately after angular load?
This is the code:
$(function() {
var p = $("#popover").popover({
content: $("#popover-content").html(),
html: true
});
});
(function () {
var app = angular.module("ng-app", []);
app.controller("NotificationsController", function ($scope, $http) {
$scope.links = [];
$scope.load = function () {
$http.get("json.php").success(function (response) {
console.log(response);
$scope.links = response;
// the response return is somethin like
// [
// {title: "Google", url: "http://www.google.com"},
// {title: "Gmail", url: "http://www.google.com"},
// ]
});
};
$scope.load();
});
})();
<ul class="nav navbar-nav">
<li data-ng-controller="NotificationsController as notification">
<a href="#" data-toggle="popover" id="popover" title="Notificações" data-placement="bottom" data-trigger="focus">
<span class="badge">{{links.length}}</span>
</a>
<div id="popover-content" style="display: none">
<div data-ng-repeat="link in links">
{{link.title}}
</div>
</div>
</li>
</ul>
Thanks in advance
You can put your function inner the controller, to get angular cycle. And, also, define your jquery call on a $timeout event.
Please look this sample on jsbin: jsbin
controller('c', function($scope, $timeout) {
$scope.getData = function() {
return $("#popover-content").html();
};
$timeout(function() {
$("#popover").popover({
content: $scope.getData,
html: true
});
});
}
$(":mobile-pagecontainer") is not working. I have to use $(document).
Anything wrong with the following code?
<div data-role="page" id="page1">
<div data-role="header" >
<h1>Page 1</h1>
</div>
<div role="main" class="ui-content">
This is Page1.
<a id="gotoPage2" href="#page2" class="ui-btn ui-corner-all ui-shadow ui-btn-a">Go to Page 2</a>
<script>
// not working
$( ":mobile-pagecontainer").on( "pagecontainerhide", function( event, ui ) {
alert( "page hide ");
});
// working
$( document).on( "pagecontainerhide", function( event, ui ) {
alert( "page hide " );
});
</script>
</div>
</page>
<page data-role="page" id="page2">
....
</page>
But it works for changing page as followings:
$(":mobile-pagecontainer").pagecontainer("change", "#page2", { } );
Thanks.
$(":mobile-pagecontainer") is a selector that refers to wrapper of all pages, internal or external. By default body is :mobile-pagecontainer and .pagecontainer() is a widget used to emit jQuery Mobile's special events and used for navigation.
jQuery Mobile events bubble up to document so you can use to capture those events.
$(document).on("pagecontainershow", function (e, data) {
console.log(data.toPage); /* current active page */
console.log(data.prevPage); /* previous page */
});
If you want to attach events to pageconatiner, you have to wrap them in .ready() in order to make them work.
$(function () {
$(":mobile-pagecontainer").on("pagecontainerhide", function (e, data) {
console.log(data.toPage); /* page navigating to */
console.log(data.prevPage); /* page that was just hidden */
});
});
It is possible also to use the widget .pagecontainer().
$(":mobile-pagecontainer").pagecontainer({
hide: function (e, data) {
/* code */
},
show: function (e, data) {
/* code */
}
});
I have a mobile app that will allow a user to tap on an item in a list, which will then populate a 'details' page, and navigate to that page.
When the user taps the back button, and selects another item in the list, the details page is updated with the new content, but the markup loses all kendo-ui-mobile styling.
Is there a way to trigger the enhanced content after the markup has been updated?
Here is my code (the relevant bits):
index.html
<div id="details" data-role="view" data-title="Details" data-layout="default">
<header data-role="header">
<div data-role="navbar">
<a id="back-button" class="nav-button" data-align="left" data-role="backbutton">Back</a>
<span data-role="view-title"></span>
</div>
</header>
<div id="details_body"></div>
</div>
router.js
$('.listItemLink').live('click', function(e) {
require(['views/companyDetailsView'], function (companyDetailsView) {
var view = new companyDetailsView({
model: companyDetails,
el: $('#details_body')
}).render(function(el) {
app.navigate('#details');
app.hideLoading();
}).el;
});
});
companyDetailsView.js
define([
'backbone',
'underscore',
'models/companyModel',
'text!templates/companyDetails.html'
], function (Backbone, _, companyModel, tmpl) {
'use strict';
return Backbone.View.extend({
tagName: 'li',
template: _.template(tmpl),
render: function (callback) {
this.$el.html(this.template(this.model.toJSON()));
callback(this.$el);
return this;
}
});
});
I've got multiple HTML pages that use the JQMobile framework; within these pages I'm using iScroll to create a native scrolling effect, all works fine.
I run into problems when using the JQM page transitions with iScroll, since it's loaded via ajax I know that I need to refresh iScroll on the new page so that it can correctly calculate the height and width after the DOM has changed.
I've looked into this and experimented with code (tried refresh() and destroying and recreating) but can't see to get it work, the iScroll works it's just not getting refreshed on page change (therefore not working), any ideas?
Code below!
<div data-role="page">
<div data-role="header">
</div><!-- /header -->
<div data-role="content">
<div id="wrapper">
<div id="scroller">
<p>Page content goes here.</p>
Page 2
</div>
</div>
</div><!-- /content -->
<div data-role="footer">
<div data-role="navbar">
<ul>
<li><a data-ajax="false" href="javascript:void(0);" onClick="homepage();"><img width="34px" height="34px" src="images/home_IMG_v2.png" /><!--<span class="nav">Home</span>--></a></li>
<li><a data-ajax="false" href="Guide.html" class="ui-btn-active ui-state-persist"><img width="35px" height="33px" src="images/guide_IMG_v2.png"><!--<span class="nav">Guide</span>--></a></li>
<li><a data-ajax="false" href="TaxCalculator.html" /><img width="76px" height="34px" src="images/calculator_IMG_v2.png" /><!--<span id="calc" class="nav">Calculator</span>--></a></li>
</ul>
</div>
</div><!-- /footer -->
</div><!-- /page -->
Using refresh()
var myScroll;
function loaded() {
myScroll = new iScroll('wrapper', { hScrollbar: false, vScrollbar: false, checkDOMChanges: true});
setTimeout(function() {
myScroll.refresh();
}, 100);
}
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
document.addEventListener('DOMContentLoaded', function () { setTimeout(loaded, 200); }, false);
Destroying iScroll and recreating
var myScroll;
function loaded() {
myScroll = new iScroll('wrapper', { hScrollbar: false, vScrollbar: false, checkDOMChanges: true});
setTimeout(function() {
myScroll.destroy();
myScroll = null;
myScroll = new iScroll('wrapper', { hScrollbar: false, vScrollbar: false, checkDOMChanges: true});
}, 100);
}
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
document.addEventListener('DOMContentLoaded', function () { setTimeout(loaded, 200); }, false);
Try calling initialising iScroll when the jqm pageshow event fires e.g. in jqm 1.3.1:
$(document).on('pageshow', function (event, ui) {
myScroll = new iScroll('wrapper', { hScrollbar: false, vScrollbar: false, checkDOMChanges: true});
});
after domchanges you need refreshing iscroll
$('#videotagisc').iscrollview("refresh");
this not working means use setTimeout
setTimeout(function(){
$('#videotagisc').iscrollview("refresh");
},100);
var width = $( window ).width();
var height = $( window ).height();
var delay = 200;
var doit;
function keop() {
$("#wrapper").css({"height":height+"px","width":width+"px"});
setTimeout( function(){
myScroll.refresh() ;
} , 200 ) ;
}
/* < JQuery 1.8*/
window.addEventListener("load", function(){
clearTimeout(doit);
doit = setTimeout(keop, delay);
});
/* > JQuery 1.8*/
window.on("load", function(){
clearTimeout(doit);
doit = setTimeout(keop, delay);
});
Trying to get an event triggered with ajax content whose parent elements were also ajax loaded.
<div id="content"><!-- NOT ajax-loaded -->
<div id="location"> <!-- #location IS ajax-loaded -->
<div id="add_location> <!-- #add_location IS ajax-loaded from a #location event -->
<input type="text" id="add_location_city_example" />
<input type="text" id="add_location_state_example" />
<input type="submit" id="add_location_confirm" />
</div>
</div>
</div>
$(function(){
$('#content').on('click', '#add_location_confirm', function(){
console.log('debug 1');
add_location();
// will not be called
});
$('#location').on('click', '#add_location_confirm', function() {
console.log('debug 2');
// will not be called either
add_location();
});
});
If I have onclick="add_location()" and function add_location() { console.log('debug 3); } in my .js then it will obviously be called BUT I then cannot get $('#add_location_city_example').val() because none of it will be in the dom.
NOTE: using 1.9.1
I've been using this for a while, makes it much easier to handle situations like you are describing + there is only one even assignment for pretty much all clicks on the page, including elements that will appear on the page in the future:
$(document).bind('click', function (e) {
var target = $(e.target);
if (target.is('#content')) {
e.preventDefault();
// do whatever
} else if (target.is('#location')) {
e.preventDefault();
// do whatever else
}
});
or in your case it would probably be more like this:
$(document).bind('click', function (e) {
var target = $(e.target);
if (target.is('#add_location_confirm')) {
e.preventDefault();
if (target.closest('#location').length == 0) { // not yet have location injected via ajax
// do something
} else {
// location has been injected, do something else
}
});