From 49ac0ab93378c51f95daad6dba32631a550e4ced Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Germade?= Date: Tue, 4 Dec 2018 03:30:55 +0100 Subject: [PATCH] avoiding move nodes when no changes in repeat --- directives/repeat.js | 15 ++++++++++++++- tests/_karma-app.js | 15 ++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/directives/repeat.js b/directives/repeat.js index 4ea7b8f..7d26f63 100644 --- a/directives/repeat.js +++ b/directives/repeat.js @@ -40,6 +40,10 @@ module.exports = function (APP, TEXT, directive_ns) { getList = TEXT.eval(matched_expressions[3]), previous_repeat = [] + function __matchesPreviousRepeat (data_item, i) { + return data_item === previous_repeat[i].data_item + } + // console.log('index_key', index_key) function _addListItem (data, data_item, index, _insert_before) { @@ -70,7 +74,7 @@ module.exports = function (APP, TEXT, directive_ns) { // console.log('_updateRenderedData', data_item, index) - parent_el.insertBefore(item.el, close_comment) + // parent_el.insertBefore(item.el, close_comment) item.rendered.updateData(_data) return item } @@ -82,6 +86,13 @@ module.exports = function (APP, TEXT, directive_ns) { index = 0 // current_repeat = [] + if( list.length === previous_repeat.length && list.every(__matchesPreviousRepeat) ) { + previous_repeat.forEach(function (item, i) { + _updateRenderedData(item, data, list[i], i) + }) + return + } + // if( !list || typeof list !== 'object' ) throw new TypeError('expression \'' + matched_expressions[3] + '\' should return an Array or an Object') // _forEach(list, function (data_item, i) { @@ -116,6 +127,8 @@ module.exports = function (APP, TEXT, directive_ns) { while( i < n && previous_repeat.length ) { item_found = _findDataItem(previous_repeat, list[i], true) + + if( item_found ) parent_el.insertBefore(item_found.el, close_comment) // if( item_found ) console.log('item_found', item_found) current_repeat.push( item_found ? _updateRenderedData(item_found, data, list[i++], index++) : diff --git a/tests/_karma-app.js b/tests/_karma-app.js index 9907d75..b2f95c6 100644 --- a/tests/_karma-app.js +++ b/tests/_karma-app.js @@ -577,6 +577,10 @@ getList = TEXT.eval(matched_expressions[3]), previous_repeat = []; + function __matchesPreviousRepeat (data_item, i) { + return data_item === previous_repeat[i].data_item + } + // console.log('index_key', index_key) function _addListItem (data, data_item, index, _insert_before) { @@ -607,7 +611,7 @@ // console.log('_updateRenderedData', data_item, index) - parent_el.insertBefore(item.el, close_comment); + // parent_el.insertBefore(item.el, close_comment) item.rendered.updateData(_data); return item } @@ -619,6 +623,13 @@ index = 0; // current_repeat = [] + if( list.length === previous_repeat.length && list.every(__matchesPreviousRepeat) ) { + previous_repeat.forEach(function (item, i) { + _updateRenderedData(item, data, list[i], i); + }); + return + } + // if( !list || typeof list !== 'object' ) throw new TypeError('expression \'' + matched_expressions[3] + '\' should return an Array or an Object') // _forEach(list, function (data_item, i) { @@ -653,6 +664,8 @@ while( i < n && previous_repeat.length ) { item_found = _findDataItem(previous_repeat, list[i], true); + + if( item_found ) parent_el.insertBefore(item_found.el, close_comment); // if( item_found ) console.log('item_found', item_found) current_repeat.push( item_found ? _updateRenderedData(item_found, data, list[i++], index++) :