How to add a AJAX Language Dropdown that reloads the current page?
I'm trying to write a language dropdown select into my header of my JQM apps. The language is handled by the server side coding (PHP/SQL); data, stored, etc. The server side part works perfectly, but I can not figure out how to handle the ajax events and the existing bindings on the current page.
I'm seeing multiple events firing off after I select the language more than once. I think it's due to the use of the "pagecreate" which I use when loading the page.
Here's the index pages 'pagecreate' (trimmed down a bit):
- $(document).on('pagecreate', '#index', function(event) {
-
- var page = event.target.id;
- self.consoleLog('pagecreate: #'+page);
- self.bindLanguage(page);
-
- var apptAnchor = null;
- $('#someListRefreshButton').on('tap', function(e){
- self.buildSomeList();
- });
-
- // Refresh SomeList
- self.buildSomeList();
- refreshSomeListId = setInterval(function() {
- self.buildSomeList();
- }, 60 * 1000);
- });
Here's the language binding code:
- bindLanguage: function(page) {
-
- var self = this;
- self.consoleLog('bindLanguage: #'+page);
-
- // On change of language select
- $(document).on("change", "select.languageDisplay", function(e) {
- self.settings.lang = e.currentTarget.value;
- self.consoleLog(self.settings.lang);
- $.getJSON(self.settings.base_url + 'someapp/ajax_set_lang/' +
- encodeURIComponent(self.settings.lang), function(response) {
- if (response.result) {
- var pageUrl = self.settings.base_url + response.controller + '/' + page;
- self.consoleLog(pageUrl);
- // Set new language to ls and settings
- self.settings.language = response.language;
- self.lsSet('language', response.language);
- // Change/Reload Page: http://api.jquerymobile.com/pagecontainer/#method-load
- $(":mobile-pagecontainer").pagecontainer('change', pageUrl,
- {allowSamePageTransition: true, showLoadMsg: true, reload: true}
- );
- }
- //$(document).off("change", "select.languageDisplay");
- });
- });
-
- },
I thought about unbinding (using .off above), but that's not going to solve the problem of the other bindings firing off more than once. I assume I need to move some of the bindings code out of the "pagecreate" and into a "pagecontainershow" - but I'm not sure how to write that code.