I am having various nightmares trying to get some ‘on click’ type events to function as I hope. Problems include multiple firing, or not firing at all. Have different problems with different solutions, so I wonder if I could just ask whether there is a ‘correct’ answer before I try and explain what happens in different scenarios (I’ve also confused myself now trying to work it out!) I think I’ve tried all the options I’ve put below. Many thanks for correct direction or alternative suggestions.
The circumstance is having elements with a particular class, of which there may be multiple on a single page, and where there may be multiple pages in the DOM containing that class (including the same page loaded more than once). edit: the contents of the page with the element needing processing will mostly have been ajax loaded (as per page-a2 below).
Examples of how I am using them include designating something for processing in a function such as: an external link, a phone number to process, scrolling to a point on a page.
General structure:
#app
.statusbar
.view.view-main
.page
.page-content
My uses within the html page:
Tel: < span class="badge link call-num-nr" data-num="01200000000">01200 000000< /span>
JS:
var $$ = Dom7;
var app = new Framework7({
root: '#app',
clicks: {
externalLinks: '.external',
},
on: {
routerAjaxStart: function(xhr){ app.preloader.show(); },
routerAjaxComplete: function(xhr){ app.preloader.hide(); },
},
routes: routes,
});
var mainView = app.views.create('.view-main', {
stackPages: 'true',
});
document.addEventListener('deviceready', function () {
window.open = cordova.InAppBrowser.open;
}, false);
$$(document).on('page:init', function (e) {
var page = e.detail;
if (page.name === 'page-a') {
// OPTION A
}
if (page.name === 'page-a2') {
app.request.get(filename, function (data) {
$$('.sop-block').html(data);
// OPTION A
});
}
if (page.name === 'page-b') {
// OPTION A
}
// OPTION B
});
// OPTION C
-
Is my best location to put the handler for the on click event A (multiple for different pages), B, or C? (I don’t need them off the index page)
-
Which is the best way of doing it:
$$(document).on('click', '.call-num-nr', function () {
calltelnr(this.dataset.num);
});
OR
$$('.call-num-nr').on('click', function () {
calltelnr(this.dataset.num);
});