Skip to content

Commit

Permalink
work on startup sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
FirstVertex committed Sep 28, 2012
1 parent 641edc1 commit 9c36e8e
Show file tree
Hide file tree
Showing 9 changed files with 478 additions and 472 deletions.
19 changes: 5 additions & 14 deletions client/app/config.js
Expand Up @@ -6,13 +6,15 @@
// this module returns a static object

define([
// needed for string.format
'util/util'
], function (Util) {

var envs = {
dev: {
serverPort: 99,
localhost: '127.0.0.1',
// will be used in local android builds, ip of your machine
serverIP: '192.168.1.101'
},
prod: {
Expand Down Expand Up @@ -47,20 +49,17 @@ define([
test: {
device: 'web',
serverAddress: getServerAddress(true),
// this is picked up by the storage module and allows to create a random namespace in localStorage, which can be useful in testing
storagePrefix: Util.randomString(),
showConsoleMessages: true
showConsoleMessages: true,
isTest: true
},
web: {
device: 'web',
serverAddress: getServerAddress(false),
storagePrefix: '',
showConsoleMessages: true
},
android: {
device: 'android',
serverAddress: getServerAddress(false),
storagePrefix: '',
showConsoleMessages: true
}
};
Expand All @@ -87,16 +86,8 @@ define([

// determine which configuration to use
var currentConfig = getBuildConfig() || getUrlConfig() || configs.current;
// the config knows what env
// the config knows whether its in prod
currentConfig.isProd = isProd;

// avoid depending on Logger, because it depends on Config!
function logMessage(message) {
if (!currentConfig.showConsoleMessages) return;
console.log(message);
}

logMessage('Config: currentConfig=' + Util.inspectObject(configs.current));

return currentConfig;
});
68 changes: 40 additions & 28 deletions client/app/main.js
@@ -1,9 +1,4 @@
// prevent deep linking. might as well do this first because it will refresh if they have a hash when visiting the page for the 1st time
if (location.hash || location.href.slice(-1) == "#") {
window.history.pushState("", document.title, window.location.pathname);
}

// SmartJs v0.1.0
// SmartJs v0.1.0
// (c) Hugh Anderson - https://github.com/hughanderson4/smartjs
// License: MIT (http://www.opensource.org/licenses/mit-license.php)

Expand All @@ -13,6 +8,10 @@ if (location.hash || location.href.slice(-1) == "#") {
// this module will publish a single event to get the app booted
// this module will require any orphan dependencies so their code is established into the ecosystem

// prevent deep linking. might as well do this first because it will refresh if they have a hash when visiting the page for the 1st time
if (location.hash || location.href.slice(-1) == "#") {
window.history.pushState("", document.title, window.location.pathname);
}

// config for requirejs, should be the 1st thing encountered by require
requirejs.config({
Expand All @@ -33,12 +32,25 @@ require([
'config',
'jquery',
'jqueryMobile'
], function (require, Config, Jquery) {
], function (require, Config, Jquery, Mobile) {

var isDeviceReady = false,
var isDeviceReady = Config.device === 'web',
isDocReady = false,
isLaunched = false;

function bootstrap(LocalStorage, Logger, Pubsub) {
Logger.log('config', Config, true);

// todo: implement browser screen
var isBrowserAllowed = true, // Browser.isPermitted()
hasMember = LocalStorage.hasLocalMember(),
localMember = isBrowserAllowed && hasMember ? LocalStorage.getLocalMember() : null,
eventName = !isBrowserAllowed ? 'error.redirect' : hasMember ? 'member.load' : 'member.none';

// this will start off the app, most likely Router will handle it
Pubsub.publish(eventName, localMember);
}

function checkLaunchConditions() {
if (!isLaunched && isDeviceReady && isDocReady) {
isLaunched = true;
Expand All @@ -49,33 +61,33 @@ require([
'router', // need to explictily load it here because nobody depends on it
'util/koHelper', // need to explictily load it here because nobody depends on it
'util/fullHeight' // need to explictily load it here because nobody depends on it
], function (LocalStorage, Logger, Pubsub) {
Logger.log('Bootstrap: app is launching');

// todo: implement browser screen
var isBrowserAllowed = true; // Browser.isPermitted()
var eventName = !isBrowserAllowed ? 'error.redirect' : LocalStorage.hasLocalMember() ? 'member.load' : 'member.none';

Pubsub.publish(eventName, LocalStorage.getLocalMember());
});
], bootstrap);
}
}

if (Config.device === 'web') {
// don't wait for deviceready event
// mobile device, wait for deviceready event fired by phonegap
function onDeviceReady() {
console.log('deviceReady');
isDeviceReady = true;
} else {
// mobile device, wait for deviceready event fired by phonegap
document.addEventListener('deviceready', function () {
isDeviceReady = true;
checkLaunchConditions();
}, false);
checkLaunchConditions();
}

// listen for doc ready
Jquery(document).ready(function () {
console.log('docready');
function onDocumentReady() {
console.log('docReady');
isDocReady = true;
checkLaunchConditions();
}

if (!isDeviceReady) {
document.addEventListener('deviceready', onDeviceReady, false);
}

Jquery(document).ready(onDocumentReady);

Jquery.extend(Mobile, {
ajaxEnabled: false,
showLoadMsg: false,
hashListeningEnabled: false,
pushStateEnabled: false
});
});
11 changes: 7 additions & 4 deletions client/app/util/storage.js
Expand Up @@ -8,19 +8,22 @@
define([
'jstorage',
'config',
'util/pubsub'
], function (Storage, Config, Pubsub) {
'util/pubsub',
'util/util'
], function (Storage, Config, Pubsub, Util) {

var storagePrefix = Config.isTest ? Util.randomString() : '';

var keys = {
MEMBER: 'member'
};

function saveToDevice(key, value) {
Storage.set(Config.storagePrefix + key, value);
Storage.set(storagePrefix + key, value);
}

function lookup(key) {
return Storage.get(Config.storagePrefix + key, null);
return Storage.get(storagePrefix + key, null);
}

function hasKey(key) {
Expand Down

0 comments on commit 9c36e8e

Please sign in to comment.