(function() { // globals MyApp.Utils = {}; // utility functions // MyApp.Utils.someUtilityFunction = function() {} // MyApp.Utils.someOtherUtilityFunction = function() {} // app functions MyApp.renderShell = function() { document.title = i18next.t('vanillaJS'); var d = $.Deferred(); //preload this template, so we can be sure the dom in rendered when we resolve ejs.preloadTemplate('/templates/main.ejs') .then(function(templateUrl) { $('.js-main-content').html(ejs.rr(templateUrl)); d.resolve(); }); return d; }; MyApp.renderHomePage = function() { $('.js-page-container').html(ejs.rr('/templates/home.ejs')); }; MyApp.renderTextPage = function() { $('.js-page-container').html(ejs.rr('/templates/sometext.ejs', { texts: [ { id: 100, description: 'Some text with id 100' }, { id: 200, description: 'Some other text with id 200. Click it!' } ] })); }; MyApp.renderAboutPage = function() { $('.js-page-container').html(ejs.rr('/templates/about.ejs')); }; // events $(document).on('click', '.js-link', function(ev) { var el = $(ev.currentTarget); var linkId = el.attr('data-id'); PNotify.success('The text you clicked had id ' + linkId + '. Maybe next time I will do something with this id.'); }); // app startup // set language i18next .use(i18nextBrowserLanguageDetector) .use(i18nextXHRBackend) .init({ detection: { order: ['querystring', 'cookie', 'navigator'], lookupQuerystring: 'lang', lookupCookie: 'current-language', caches: ['cookie'], cookieMinutes: 5256000 }, whitelist: ['en', 'de', 'it'], load: 'languageOnly', fallbackLng: 'it', backend: { loadPath: '/i18n/{{lng}}.json' } }) .then(function() { // language initialized // if there is a lang query string parameter, remove it and reload: we save the language in a cookie so the url stays nice var urlQueryString = new URLSearchParams(window.location.search); if (urlQueryString.has('lang')) { urlQueryString.delete('lang'); var newUrl = [location.protocol, '//', location.host, location.pathname].join('') + (urlQueryString.toString() !== '' ? '?' + urlQueryString.toString() : '') + window.location.hash; window.location = newUrl; return; } // render main shell MyApp.renderShell() .then(function() { //setup routing page('/', MyApp.renderHomePage); page('/about', MyApp.renderAboutPage); page('/text', MyApp.renderTextPage); page({ hashbang: true }); }); }); })();