From 7136212b5ea29ae8dd6ffd8fc25849c93d9afef2 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Thu, 4 May 2017 14:17:28 +0200 Subject: [PATCH] made load substates syncronous to ensure everything is loaded in the right order --- lib/system/lib/system.js | 67 +++++++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 15 deletions(-) diff --git a/lib/system/lib/system.js b/lib/system/lib/system.js index 410523c..62e7a7f 100644 --- a/lib/system/lib/system.js +++ b/lib/system/lib/system.js @@ -140,6 +140,33 @@ SYSTEM.prototype.handle_call_pages_entry = function (entry,id,forced,cached, cal callback(); } } + +/** + * Process an array of data synchronously. + * + * @param data An array of data. + * @param processData A function that processes an item of data. + * Signature: function(item, i, callback), where {@code item} is the i'th item, + * {@code i} is the loop index value and {@code calback} is the + * parameterless function to call on completion of processing an item. + */ +function doSynchronousLoop(data, processData, done) { + if (data.length > 0) { + var loop = function(data, i, processData, done) { + processData(data[i], i, function() { + if (++i < data.length) { + loop(data, i, processData, done); + } else { + done(); + } + }); + }; + loop(data, 0, processData, done); + } else { + done(); + } +} + //internal function to handle pagestate results SYSTEM.prototype.handle_call_pages = function (data,id,forced,cached) { var hash = null; @@ -165,22 +192,32 @@ SYSTEM.prototype.handle_call_pages = function (data,id,forced,cached) { id !== this.cur_state()){//new state? window.history.pushState(null, "", '#!'+id+(hash ? '#'+hash : null));} - var count = []; - data['result'].forEach( - function(entry){ - system.handle_call_pages_entry(entry,id,forced,cached, - function(d){ - count.push(true); - if(count.length === data['result'].length){ - if(hash && $('#'+hash).length){ - $(document.body).animate({'scrollTop': $('#'+hash).offset().top-50}, 750); - } else { - $(document.body).animate({'scrollTop': 0}, 750); - } + //Syncronous Call to system.handle_call_pages_entry and done-function + var done = function(){ + if(hash && $('#'+hash).length){ + $(document.body).animate({'scrollTop': $('#'+hash).offset().top-50}, 750); + } else { + $(document.body).animate({'scrollTop': 0}, 750); } - } - ); - }); + }; + var data_ = data['result']; + var process = system.handle_call_pages_entry; + if (data_.length > 0) { + var loop = function(data_, i, process, done) { + process(data_[i], id,forced,cached, function() { + if (++i < data_.length) { + loop(data_, i, process, done); + } else { + done(); + } + }); + }; + loop(data_, 0, process, done); + } else { + done(); + } + + } else { this.log_info('Problem with your Pages: '+data['result']['message']); }