ef27b6804f49cf9b619524c7ad172a3d545d0494
[samples/web/EventManager.git] / js / main.js
1 /*
2  *      Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  *      Licensed under the Flora License, Version 1.0 (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
30 var app = null;
31
32 (function () { // strict mode wrapper
33     'use strict';
34
35     ({
36         /**
37          * Loader init - load the App constructor
38          */
39         init: function init() {
40             console.log('Loader init()');
41             var self = this;
42             $.getScript('js/app.js')
43                 .done(function onAppLoaded() {
44                     // once the app is loaded, create the app object
45                     // and load the libraries
46                     app = new App();
47                     self.loadLibs();
48                 })
49                 .fail(this.onGetScriptError);
50         },
51         /**
52          * Load dependencies
53          */
54         loadLibs: function loadLibs() {
55             console.log('Loader loadLibs()');
56             var loadedLibs = 0;
57             if ($.isArray(app.requires)) {
58                 $.each(app.requires, function onLibLoaded(index, filename) {
59                     console.log('Trying to load ' + filename + '...');
60                     $.getScript(filename)
61                         .done(function () {
62                             loadedLibs += 1;
63                             console.log(loadedLibs + ' libs loaded');
64                             if (loadedLibs >= app.requires.length) {
65                                 // All dependencies are loaded - initialise the app
66                                 console.log('App.init()');
67                                 app.init();
68                             }
69                         })
70                         .fail(function (e) {
71                             console.error('Loading libs failed');
72                             console.log(e);
73                         });
74                 });
75             }
76         },
77         /**
78          * Handle ajax errors
79          */
80         onGetScriptError: function onGetScriptError(e, jqxhr, setting, exception) {
81             alert('An error occurred: ' + e.message);
82         }
83     }).init(); // run the loader
84
85 }());