From ff09f673ceb6742f5caf81bb53f0c552eaaad0ca Mon Sep 17 00:00:00 2001 From: Eddie Wen Date: Sun, 5 Feb 2017 20:04:54 +0800 Subject: [PATCH] Refactor loadMoreFw with ES6 And make sure data array is loaded --- src/fallwall.js | 63 ++++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/src/fallwall.js b/src/fallwall.js index 03ad3ba..36e01a9 100644 --- a/src/fallwall.js +++ b/src/fallwall.js @@ -17,6 +17,7 @@ columnNumber: 1, defaultClass: '', currentGrid: 0, + dataArray: [], }; /** @@ -144,51 +145,65 @@ /** * load more data and append them */ - $.fn.loadMoreFw = function( callback_func ) { + $.fn.loadMoreFw = ( callbackFunction ) => { - if( defaults.currentGrid +1 < defaults.dataArray.length ) { + /** + * fallwall initialization is not yet completed + */ + if( defaults.dataArray.length === 0 ) { + return; + } + + if( defaults.currentGrid + 1 < defaults.dataArray.length ) { defaults.currentGrid++; - var limitNum = defaults.currentGrid + defaults.gridNumber; - for( var i = defaults.currentGrid; i < limitNum; i++ ) { + const limitNum = defaults.currentGrid + defaults.gridNumber; + for( let i = defaults.currentGrid; i < limitNum; i++ ) { - if( typeof defaults.dataArray[i] != "undefined" ) { + if( defaults.dataArray[i] ) { _appendGrids( defaults.dataArray[i], 'down' ); defaults.currentGrid = i; } else { - // Data is exhausted before last run in loop - if( callback_func ) { - if( typeof callback_func == 'function' ) - callback_func(); - else - console.error(callback_func+' is not a function'); + /** + * Data is exhausted before last run in loop + */ + if( callbackFunction ) { + if( typeof callbackFunction === 'function' ) { + callbackFunction(); + } + else { + console.error(`${callbackFunction} is not a function`); + } } - return "NO_MORE_DATA"; + return 'NO_MORE_DATA'; } - // Last run in loop - if( i == limitNum-1 ) { - if( callback_func ) { - if( typeof callback_func == 'function' ) - callback_func(); - else - console.error(callback_func+' is not a function'); + /** + * it is the last run of this loop + */ + if( i === limitNum - 1 ) { + if( callbackFunction ) { + if( typeof callbackFunction === 'function' ) { + callbackFunction(); + } + else { + console.error(`${callbackFunction} is not a function`); + } } - return "FINISHED"; + return 'FINISHED'; } } } - /*** + /** * There is no more data. - * All is displayed. - ***/ - return "NO_MORE_DATA"; + */ + return 'NO_MORE_DATA'; };