$(document).ready( function( $ ){ //global settings objects //write settings to localStorage if so function Collection(){ this.storageAvailable = Modernizr.localstorage; this.defaultNull = ""; //otherwise, set the settings and session //store a few values for use as limits this.widthOpen = '90%'; this.widthClosed = '4.99%'; this.speed = 200; this.defaultColor = "rgba( 15, 92, 172, 0.5 )"; this.activeColor = "rgba( 255, 255, 255, 0.6 )"; this.hideBackgroundImage = '-10000px -10000px'; this.showBackgroundImage = 'right'; //off the bat, set a debug flag (lots of very verbose console messages if set to true) this.debug = false; //could usefully be true or false and "level2" or "level3" //This object is used EVERYWHERE... this.liveInfo = new Object(); //create a few default values that always should be present for the application //set todays date and instantiate moment var now = moment(new Date()); //write to storage this.liveInfo.currentDate = now.toDate(); //push 30 days forward onto storage this.liveInfo.futureDate = now.add("days", 30).format("YYYY-MM-DD"); //set flag for use as a content switch this.liveInfo.setOriginalCallDate = false; //set defaults for used values this.liveInfo.originalDownload = ''; this.liveInfo.returningUser = ''; this.liveInfo.userAddress = ''; this.liveInfo.googleMapsOriginAddresses = ''; this.liveInfo.googleMapsDestinations = ''; this.liveInfo.userInput = ''; this.liveInfo.nameOfSelf = ''; this.saveAll = function(){ console.log( "saveAll Method not yet implemented." ); } this.saveLiveInfo = function(){ try{ if( !this.storageAvailable ) throw 'no localStorage available - writing to cookie'; //doing it the cheap way to get this done, //ideally we would walk through the object and store one by one //so that we could pull one property as needed. //currently it is a huge object with the full downloaded mealsites listing localStorage.removeItem( 'liveInfo' ); var serialized = JSON.stringify( this.liveInfo ); localStorage.setItem( 'liveInfo', serialized ); return true; /* for( property in this.liveInfo ){ localstorage[ this ] = this[ property ]; } for( property in object ){ localStorage[ "liveInfo" ][ property ] = this[ "liveInfo" ][ property ]; } */ return true; }catch( error ){ console.log( error ); //cookie fallback //@todo throw new error and switch to cookies there var tempObject = $.extend( true, {}, this.liveInfo ); tempObject.originalDownload = ''; tempObject.googleMapsDestinations = '' var tempString = JSON.stringify( tempObject ); $.cookie( 'smallInfo', tempString, { expires: 365, path: '/' } ); return true; } } this.saveOne = function( property ){ localStorage[ property ] = this[ property ]; return true; } this.readLiveInfo = function(){ try{ if( !this.storageAvailable ) throw 'no localStorage available - reading from cookie'; var tempLiveInfo = localStorage.getItem( 'liveInfo' ); this.liveInfo = JSON.parse( tempLiveInfo ); /* for( property in this ){ this[ "liveInfo" ][ property ] = localStorage[ "liveInfo" ][ property ] ; } */ return true; }catch( error ){ console.log( error ); console.log( 'can\'t read from disk. Attempting to read from cookie' ); var tempCookie = $.cookie( 'smallInfo' ); var tempJson = JSON.parse( tempCookie ); for( property in tempJson ){ this.liveInfo[ property ] = tempJson[ property ]; } } } this.readAll = function(){ console.log( 'readAll method not implemented. Feel free to override :-)' ); } this.readOne = function(){ this[ property ] = localStorage[ property ]; return this[ property ]; } this.countAll = function(){ var count = 0; for( item in this ){ if( item != "undefined" ) count++; } return count; } this.test = function(){ try{ console.log( "This is the current contents of the " + this.nameOfSelf + " Collection:" ); console.log( this ); return true; }catch( error ){ console.log( error ); } } this.liveTest = function(){ try{ console.log( "This is the current contents of the liveInfo object:" ); console.log( this.liveInfo ); }catch( error ){ console.log( error ); } } } //persist object is used for nearly all settings and storage... var persist = new Collection(); persist.nameOfSelf = "persist" persist.test(); persist.liveTest(); //eventListener for value changes in localStorage $( document ).bind( 'storage', function( event ){ console.log( event ); }); //very heavily informed dom event listener if( persist.debug == 'level3' ){ //set a event listener to report the items being put into the dom. var thisDOMEventCounter = 0; //only for debugging purposes $( document ).bind( "DOMNodeInserted", function( event ){ console.log(thisDOMEventCounter + '----------------------------------------------------Event Target is:'); console.log(event.target); console.log(thisDOMEventCounter + '----------------------------------------------------event.target.textContent is:'); console.log(event.target.textContent.replace(/(\r\n|\n|\r|\s+)/gm, " ")); console.log(thisDOMEventCounter + '----------------------------------------------------The whole Event object is:'); console.log(event); thisDOMEventCounter++; }); } try{ //if we don't have the download already local, pull one if( persist.liveInfo.originalDownload == '' ){ //else pull a document from the server (placeholder for actual ajax call to 2-1-1 website) $.get('sample-mealsites.html', function( data ){ //make a copy of download into global settings object persist.liveInfo.originalDownload = data.replace(/(\r\n|\n|\r|\s+)/gm, " ").trim(); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Uncaught TypeError: Cannot set property 'originalDownload' of null ___________________________________________________________________________________________________________ //display placeholder (temporary, but needed if either google or facebook api fail) $( '#content > div > div:first' ).append( persist.liveInfo.originalDownload ); }); } else { //just use the local one $( '#content > div > div:first' ).append( persist.liveInfo.originalDownload ); } }catch( error ){ console.log( error ) }