diff options
Diffstat (limited to 'js/main.js')
-rw-r--r-- | js/main.js | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/js/main.js b/js/main.js new file mode 100644 index 0000000..ef27b68 --- /dev/null +++ b/js/main.js @@ -0,0 +1,85 @@ +/* + * Copyright 2012 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/*jslint devel: true*/ +/*global $, tizen, App */ + +/** + * This file acts as a loader for the application and its dependencies + * + * First, the 'app.js' script is loaded . + * Then, scripts defined in 'app.requires' are loaded. + * Finally, the app is initialised - the app is instantiated ('app = new App()') + * and 'app.init()' is called. + */ + + +var app = null; + +(function () { // strict mode wrapper + 'use strict'; + + ({ + /** + * Loader init - load the App constructor + */ + init: function init() { + console.log('Loader init()'); + var self = this; + $.getScript('js/app.js') + .done(function onAppLoaded() { + // once the app is loaded, create the app object + // and load the libraries + app = new App(); + self.loadLibs(); + }) + .fail(this.onGetScriptError); + }, + /** + * Load dependencies + */ + loadLibs: function loadLibs() { + console.log('Loader loadLibs()'); + var loadedLibs = 0; + if ($.isArray(app.requires)) { + $.each(app.requires, function onLibLoaded(index, filename) { + console.log('Trying to load ' + filename + '...'); + $.getScript(filename) + .done(function () { + loadedLibs += 1; + console.log(loadedLibs + ' libs loaded'); + if (loadedLibs >= app.requires.length) { + // All dependencies are loaded - initialise the app + console.log('App.init()'); + app.init(); + } + }) + .fail(function (e) { + console.error('Loading libs failed'); + console.log(e); + }); + }); + } + }, + /** + * Handle ajax errors + */ + onGetScriptError: function onGetScriptError(e, jqxhr, setting, exception) { + alert('An error occurred: ' + e.message); + } + }).init(); // run the loader + +}());
\ No newline at end of file |