UNPKG

@danielkalen/simplybind

Version:

Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.

29 lines (22 loc) 992 B
addToNodeStore = (nodeStore, node, targetPlaceholder)-> nodeStore[targetPlaceholder] ?= [] nodeStore[targetPlaceholder].push(node) return scanTextNodesPlaceholders = (element, nodeStore)-> childNodes = Array::slice.call(element.childNodes) for node in childNodes if node.nodeType isnt 3 scanTextNodesPlaceholders(node, nodeStore) else if node.textContent.match(pholderRegExSplit) textContent = node.textContent textPieces = textContent.split(pholderRegEx) if textPieces.length is 3 and textPieces[0]+textPieces[2] is '' addToNodeStore(nodeStore, node, textPieces[1]) else newFragment = document.createDocumentFragment() for textPiece,index in textPieces newNode = newFragment.appendChild document.createTextNode(textPiece) if index % 2 # is an odd index, indicating that before this text piece should come a placeholder node addToNodeStore(nodeStore, newNode, textPiece) node.parentNode.replaceChild(newFragment, node) return