I dissected ng-repeat and extracted the code blocks attached, seeing that these comprise the logic that handles the repeating algorithm (which I want to und
After some tinkering with the directive, I became familiar with ng-repeater
s code, and managed to answer some of my questions. I highlighted in bold the things I couldn't yet figure out on my own, and would appreciate if anyone could shed some light on the bold parts:
hasOwnProperty
, because they use that method to check whether the ID is present in the iteration objects (lastBlockMap
, nextBlockMap
) (this process explained below). I couldn't find out on what scenario this can actually happen, however.nextBlockMap
contains all items that will be transcluded on the current model change. lastBlockMap
contains everything from the previous model update. It used for finding duplicates in the collection.for
loop, ng-repeat
fills up nextBlockMap
with items from lastBlockMap
. Looking at the order of if
s, it's easy to see that if the item cannot be found in lastBlockMap
, but it is already present in nextBlockMap
(meaning, it was already copied there from lastBlockMap
, and therefore its trackById
appears twice in the collection) - it's a duplicate. What the forEach
does is simply run through all initialized items in nextBlockMap
(block
s that have a startNode
property) and push their ID back into lastBlockMap
. I cannot however understand why this is necessary.nextBlockOrder
(all trackById
s in an array) from nextBlockMap
(all block
objects in a trackById
hash), is this line, which working with an array makes it an easy and simple operation: if (nextBlockOrder[index - 1]) previousNode = nextBlockOrder[index - 1].endNode;
. It is explained in the answers to question 5 and 6:block.startNode
and block.endNode
are the first and last DOM nodes in the block belonging to an item in the collected being repeated. Therefore, this line here sets previousNode
to reference the last DOM node of the previous item in the repeater.previousNode
is then used as the first node, in a loop that checks how the DOM changed when items have been moved around or removed from the repeater collection - again, only in case we are not working with the first block in the array.$scope
and startNode
and endNode
for later reference, and saves everything in nextBlockMap
. The comment created right after the cloned element, is there to guarantee we always have an endNode
.