Updated application sources
[apps/web/sample/ExercisePlanner.git] / project / js / main.js
1 /*
2  *      Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  *      Licensed under the Flora License, Version 1.1 (the "License");
5  *      you may not use this file except in compliance with the License.
6  *      You may obtain a copy of the License at
7  *
8  *              http://floralicense.org/license/
9  *
10  *      Unless required by applicable law or agreed to in writing, software
11  *      distributed under the License is distributed on an "AS IS" BASIS,
12  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *      See the License for the specific language governing permissions and
14  *      limitations under the License.
15  */
16
17 /*jslint devel: true*/
18 /*global $, tizen, App  */
19
20 /**
21  * This file acts as a loader for the application and its dependencies
22  *
23  * First, the 'app.js' script is loaded .
24  * Then, scripts defined in 'app.requires' are loaded.
25  * Finally, the app is initialised - the app is instantiated ('app = new App()')
26  * and 'app.init()' is called.
27  */
28
29 var app = null;
30
31 (function () { // strict mode wrapper
32     'use strict';
33
34     ({
35         /**
36          * Loader init - load the App constructor
37          */
38         init: function init() {
39             var self = this;
40             $.getScript('js/app.js').done(function onAppLoaded() {
41                 // once the app is loaded, create the app object
42                 // and load the libraries
43                 app = new App();
44                 self.loadLibs();
45             }).fail(this.onGetScriptError);
46         },
47         /**
48          * Load dependencies
49          */
50         loadLibs: function loadLibs() {
51             var loadedLibs = 0;
52             if ($.isArray(app.requires)) {
53                 $.each(app.requires, function onLibLoaded(index, filename) {
54                     $.getScript(filename).done(function () {
55                         loadedLibs += 1;
56                         if (loadedLibs >= app.requires.length) {
57                             // All dependencies are loaded - initialise the app
58                             app.init();
59                         }
60                     }).fail(function (e) {
61                         console.error('Loading libs failed');
62                     });
63                 });
64             }
65         },
66         /**
67          * Handle ajax errors
68          */
69         onGetScriptError: function onGetScriptError(
70             e,
71             jqxhr,
72             setting,
73             exception
74         ) {
75             alert('An error occurred: ' + e.message);
76         }
77     }).init(); // run the loader
78
79 }());