From bc4f004fa4f359a1d993d3a7008f1fbed8716a31 Mon Sep 17 00:00:00 2001 From: rylon Date: Sun, 5 Apr 2015 05:34:42 +0200 Subject: [PATCH] state system, no double load on already loaded content, redirect to start state if unknown state is requested --- files/sys/system.js | 75 ++++++++++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 31 deletions(-) diff --git a/files/sys/system.js b/files/sys/system.js index c02a0fa..5d018c4 100644 --- a/files/sys/system.js +++ b/files/sys/system.js @@ -11,6 +11,7 @@ function SYSTEM(endpoint, group,start_state){ this.endpoint = endpoint; this.group = group; this.pages = null; + this.state = {}; this.start_state = start_state; this.go_state(start_state); @@ -21,40 +22,52 @@ function SYSTEM(endpoint, group,start_state){ SYSTEM.prototype.handle_call_pages = function (data,id) { if(data['status']){ system.log_info('load pages: endpoint '+system.endpoint+' group:'+system.group+' state:'+id+' - success'); + //state not found? + if(data['result'].length === 0){ + system.log_error('load pages: endpoint '+system.endpoint+' group:'+system.group+' state:'+id+' - state not found - redirecting to start state: '+system.start_state); + system.load(system.start_state); + return;} if(id !== system.cur_state()){ window.history.pushState(null, "", '#!'+id);} data['result'].forEach(function(entry) { - //load pages - $.ajax({ - async: false, - data: {}, - dataType: 'html', - url: entry['url']+'&'+window.location.search.substr(1), - success: function(data){ - $(entry['div']).html(data); - system.log(system.LOG_INFO,'load page: '+id+entry['div']+' '+entry['url']+'&'+window.location.search.substr(1)+' - success');}, - error: function(XMLHttpRequest, textStatus, errorThrown){system.log(system.LOG_ERROR,errorThrown);} - }); - //load css - for(var i=0; i < entry['css'].length; i++){ - system.load_css(entry['css'][i]);} - //load js - var call_func = true; - var loaded = 0; - for(var i=0; i < entry['js'].length; i++){ - system.log(system.LOG_INFO,'load js: '+entry['js'][i]); - $.getScript(entry['js'][i]).done(function(response, status) { - system.log(system.LOG_INFO,'load js: '+status); - if(loaded++ == entry['js'].length-1){ - var fn = window[entry['func']]; - if(call_func && typeof fn === 'function'){ - call_func = false; - fn(); - system.log_info('call func: '+entry['func']); - } else { - system.log_error('call func: '+entry['func']+' - fail'); - }} - }); + //check loaded state of div - reload only if required + if(system.state[entry['div']] !== entry['url']+'&'+window.location.search.substr(1)){ + //load pages + $.ajax({ + async: false, + data: {}, + dataType: 'html', + url: entry['url']+'&'+window.location.search.substr(1), + success: function(data){ + $(entry['div']).html(data); + system.log(system.LOG_INFO,'load page: '+id+entry['div']+' '+entry['url']+'&'+window.location.search.substr(1)+' - success');}, + error: function(XMLHttpRequest, textStatus, errorThrown){system.log(system.LOG_ERROR,errorThrown);} + }); + //load css + for(var i=0; i < entry['css'].length; i++){ + system.load_css(entry['css'][i]);} + //load js + var call_func = true; + var loaded = 0; + for(var i=0; i < entry['js'].length; i++){ + system.log(system.LOG_INFO,'load js: '+entry['js'][i]); + $.getScript(entry['js'][i]).done(function(response, status) { + system.log(system.LOG_INFO,'load js: '+status); + if(loaded++ === entry['js'].length-1){ + var fn = window[entry['func']]; + if(call_func && typeof fn === 'function'){ + call_func = false; + fn(); + system.log_info('call func: '+entry['func']); + } else { + system.log_error('call func: '+entry['func']+' - fail'); + }} + }); + } + //update state + system.state[entry['div']] = entry['url']+'&'+window.location.search.substr(1); + } else { + system.log(system.LOG_INFO,'load page: '+id+entry['div']+' '+entry['url']+'&'+window.location.search.substr(1)+' - skipped - already loaded'); } }); } else {