Skip to content

Commit

Permalink
avoiding move nodes when no changes in repeat
Browse files Browse the repository at this point in the history
  • Loading branch information
jgermade committed Dec 4, 2018
1 parent dd3721e commit 49ac0ab
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
15 changes: 14 additions & 1 deletion directives/repeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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++) :
Expand Down
15 changes: 14 additions & 1 deletion tests/_karma-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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++) :
Expand Down

0 comments on commit 49ac0ab

Please sign in to comment.