if (typeof(console) == 'undefined') { console = { log: function() { return null; } }; } if (typeof(history.pushState) === 'undefined') { history.pushState = function() { return null; } history.replaceState = function() { return null; } } else { window.onpopstate = function (e) { if (App._init) { return App.loadPage(); } } } var App = { service: '/api/', cached: {}, cart: {}, community: null, page: {}, config: {}, _init: false }; if (typeof(Ti) != 'undefined') { App.request = function(url, complete) { var client = Ti.Network.createHTTPClient(); client.open("GET", url, false); client.send(); console.log('[REQUEST]',url); console.log('[RESPONSE]',client.responseText); complete($.parseJSON(client.responseText),client.responseText); }; } else { App.request = function(url, complete) { var triggerCache = arguments[2] ? true : false; $.getJSON(url,function(json) { if (triggerCache) { $('body').triggerHandler('cache-item-loaded-' + arguments[2]); } complete(json); }); }; } App.itemLoaded = function(type) { $('body').triggerHandler('cache-item-loaded-' + type); }; // quick and dirty App.cache = function(type, id) { var finalid, args = arguments; if (arguments[2]) { $('body').one('cache-item-loaded-' + type, function() { args[2](); }); } if (typeof(id) == 'object') { console.log ('storing object',type,id); App.cached[type][id.id] = id; eval('App.cached[type][id.id] = new '+type+'(id)'); finalid = id.id; $('body').triggerHandler('cache-item-loaded-' + type); } else if (!App.cached[type][id]) { console.log ('creating from network',type,id); eval('App.cached[type][id] = new '+type+'(id)'); } else { console.log ('loading from cache',type,id); $('body').triggerHandler('cache-item-loaded-' + type); } // only works sync (Ti) return App.cached[type][finalid || id]; }; App.loadRestaurant = function(id) { var loc = '/' + App.community.permalink + '/' + id; history.pushState({}, loc, loc); App.loadPage(); }; App.loadCommunity = function(id) { App.cache('Community',id, function() { App.community = App.cached['Community'][id]; if (!App.community.id_community) { $('.main-content').show(); $('.main-content').html('invalid community'); return; } for (var x in App.cached['Community']) { App.cached['Community'][App.cached['Community'][x].permalink] = App.cached['Community'][x]; App.cached['Community'][App.cached['Community'][x].id_community] = App.cached['Community'][x]; } for (var x in App.cached['Restaurant']) { App.cached['Restaurant'][App.cached['Restaurant'][x].permalink] = App.cached['Restaurant'][x]; } App.loadPage(); }); }; App.page.community = function(id) { // probably dont need this since we force community to be loaded //App.community = App.cache('Community', id, function() { document.title = 'Crunchbutton - ' + App.community.name; $('.main-content').html( '

one click food ordering

' + '
'); var rs = App.community.restaurants(); if (rs.length == 4) { $('.content').addClass('short-meal-list'); } else { $('.content').removeClass('short-meal-list'); } for (var x in rs) { var restaurant = $('
'); var restaurantContent = $('
'); restaurantContent .append('
') .append('

' + rs[x]['name'] + '

') .append('

Top Item: ' + rs[x].top().name + '

'); restaurant .append('
') .append(restaurantContent); $('.meal-items').append(restaurant); } //}); }; App.page.restaurant = function(id) { $('.content').addClass('short-meal-list'); App.cache('Restaurant', id, function() { if (App.restaurant && App.restaurant.permalink != id) { App.cart.resetOrder(); } App.restaurant = App.cached['Restaurant'][id]; document.title = App.restaurant.name; $('.main-content').html( '

' + App.restaurant.name + '

' + '
' + '
' + '
' + '
' + '
' + '
' ); if (!App.config.user.id_user) { $('.main-content').append(''); } else { $('.main-content').append(''); } $('.restaurant-items').append( '
top items
' + '' + '
top sides
' + '