state system, no double load on already loaded content, redirect to start state if unknown state is requested
This commit is contained in:
parent
852e26dd2f
commit
bc4f004fa4
@ -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 {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user