state system, no double load on already loaded content, redirect to start state if unknown state is requested

This commit is contained in:
Ulf Gebhardt 2015-04-05 05:34:42 +02:00
parent 852e26dd2f
commit bc4f004fa4

View File

@ -11,6 +11,7 @@ function SYSTEM(endpoint, group,start_state){
this.endpoint = endpoint; this.endpoint = endpoint;
this.group = group; this.group = group;
this.pages = null; this.pages = null;
this.state = {};
this.start_state = start_state; this.start_state = start_state;
this.go_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) { SYSTEM.prototype.handle_call_pages = function (data,id) {
if(data['status']){ if(data['status']){
system.log_info('load pages: endpoint '+system.endpoint+' group:'+system.group+' state:'+id+' - success'); 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()){ if(id !== system.cur_state()){
window.history.pushState(null, "", '#!'+id);} window.history.pushState(null, "", '#!'+id);}
data['result'].forEach(function(entry) { data['result'].forEach(function(entry) {
//load pages //check loaded state of div - reload only if required
$.ajax({ if(system.state[entry['div']] !== entry['url']+'&'+window.location.search.substr(1)){
async: false, //load pages
data: {}, $.ajax({
dataType: 'html', async: false,
url: entry['url']+'&'+window.location.search.substr(1), data: {},
success: function(data){ dataType: 'html',
$(entry['div']).html(data); url: entry['url']+'&'+window.location.search.substr(1),
system.log(system.LOG_INFO,'load page: '+id+entry['div']+' '+entry['url']+'&'+window.location.search.substr(1)+' - success');}, success: function(data){
error: function(XMLHttpRequest, textStatus, errorThrown){system.log(system.LOG_ERROR,errorThrown);} $(entry['div']).html(data);
}); system.log(system.LOG_INFO,'load page: '+id+entry['div']+' '+entry['url']+'&'+window.location.search.substr(1)+' - success');},
//load css error: function(XMLHttpRequest, textStatus, errorThrown){system.log(system.LOG_ERROR,errorThrown);}
for(var i=0; i < entry['css'].length; i++){ });
system.load_css(entry['css'][i]);} //load css
//load js for(var i=0; i < entry['css'].length; i++){
var call_func = true; system.load_css(entry['css'][i]);}
var loaded = 0; //load js
for(var i=0; i < entry['js'].length; i++){ var call_func = true;
system.log(system.LOG_INFO,'load js: '+entry['js'][i]); var loaded = 0;
$.getScript(entry['js'][i]).done(function(response, status) { for(var i=0; i < entry['js'].length; i++){
system.log(system.LOG_INFO,'load js: '+status); system.log(system.LOG_INFO,'load js: '+entry['js'][i]);
if(loaded++ == entry['js'].length-1){ $.getScript(entry['js'][i]).done(function(response, status) {
var fn = window[entry['func']]; system.log(system.LOG_INFO,'load js: '+status);
if(call_func && typeof fn === 'function'){ if(loaded++ === entry['js'].length-1){
call_func = false; var fn = window[entry['func']];
fn(); if(call_func && typeof fn === 'function'){
system.log_info('call func: '+entry['func']); call_func = false;
} else { fn();
system.log_error('call func: '+entry['func']+' - fail'); 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 { } else {