Created
November 7, 2012 14:49
-
-
Save Saneyan/4032023 to your computer and use it in GitHub Desktop.
Application starter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * start.app.js | |
| * | |
| * Application starter. Will use it on my website :) | |
| * | |
| * @author Saneyuki Tadokoro (@Saneyan or @jSaneyan) <post@saneyuki.gfunction.com> | |
| */ | |
| // | |
| // Use strict mode | |
| // | |
| "use strict"; | |
| // | |
| // Define application | |
| // | |
| var Application = function( navi, doc ){ | |
| // Regular express for file path | |
| var pathRegExp = /^$/; | |
| /* | |
| * Require a script (Local scope) | |
| */ | |
| var _require = function( src, success, error ){ | |
| var script = doc.createElement( 'script' ); | |
| script.type = 'text/javascript'; | |
| script.src = src; | |
| script.onload = success; | |
| script.onerror = error; | |
| doc.getElementsByTagName( 'head' ).item( 0 ).appendChild( script ); | |
| }; | |
| // Application instance will be in this variable | |
| var _instance; | |
| /* | |
| * Application constructor | |
| */ | |
| var _constructor = function(){ | |
| //console.log( "Created 'Application' instance." ); | |
| }; | |
| _constructor.prototype = { | |
| /* | |
| * Try to start Dart VM | |
| */ | |
| startDart: function(){ | |
| if( navi.webkitStartDart ){ | |
| if( !navi.webkitStartDart() ) | |
| throw "Could not startup Dart VM <start.app.js>"; | |
| } | |
| else{ | |
| throw "This browser doesn't have Dart VM <start.app.js>"; | |
| } | |
| } | |
| /* | |
| * Require script | |
| * @param string path (Must be file path) | |
| * @param function success <optional> (Will be called when the script is loaded successfully) | |
| * @param function error <optional> (Will be called when it cannot load the script) | |
| */ | |
| , require: function( path, success, error ){ | |
| path = path.replace( pathRegExp, '' ); | |
| _require( path, success, function(){ | |
| if( typeof error == 'function' ) | |
| error(); | |
| throw "Could not load a script '" + path + "' <start.app.js>"; | |
| }); | |
| } | |
| }; | |
| _constructor.getInstance = function(){ | |
| if( !( _instance instanceof this ) ) | |
| _instance = new this(); | |
| return _instance; | |
| }; | |
| return _constructor; | |
| }( navigator, document ); | |
| window.addEventListener( 'load', function(){ | |
| // Get 'Application' instance | |
| var app = Application.getInstance(); | |
| try{ | |
| // Try to start Dart VM | |
| app.startDart(); | |
| } catch( e ){ | |
| // Otherwise, try to load and start compatible scripts | |
| var _success = function(){ console.log( "Success :)" ); }; | |
| var _error = function(){ console.log( "Error :(" ); }; | |
| app.require( 'scripts/js/compatible.app.js', _success, _error ); | |
| } | |
| // Singleton pattern test | |
| var app2 = Application.getInstance(); | |
| // Result... | |
| console.log( "app === app2? " + ( app === app2 ) ); // app === app2? true | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment