X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=project%2Fjs%2Fmain.js;h=c302f820ef1244195628e0a3ee3265c0e1105961;hb=refs%2Fchanges%2F66%2F22366%2F1;hp=f928da4e51c6937ff9f687587a3123625c2ebf39;hpb=f29ae5cb70531a361123fb2e74b3aaac59127693;p=apps%2Fweb%2Fsample%2FExercisePlanner.git diff --git a/project/js/main.js b/project/js/main.js index f928da4..c302f82 100644 --- a/project/js/main.js +++ b/project/js/main.js @@ -13,9 +13,67 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/*global ExercisePlanner, tizen, $, document*/ -var c = null, exercisePlanner = null, appService = null, ui = null; -exercisePlanner = new ExercisePlanner(); -$(document).ready(exercisePlanner.init.bind(exercisePlanner)); +/*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() { + 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() { + var loadedLibs = 0; + if ($.isArray(app.requires)) { + $.each(app.requires, function onLibLoaded(index, filename) { + $.getScript(filename).done(function () { + loadedLibs += 1; + if (loadedLibs >= app.requires.length) { + // All dependencies are loaded - initialise the app + app.init(); + } + }).fail(function (e) { + console.error('Loading libs failed'); + }); + }); + } + }, + /** + * Handle ajax errors + */ + onGetScriptError: function onGetScriptError( + e, + jqxhr, + setting, + exception + ) { + alert('An error occurred: ' + e.message); + } + }).init(); // run the loader + +}());