Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix($compile): fix replaceWith
Browse files Browse the repository at this point in the history
the old implementation didn't reattach jquery/jqlite data which caused
things like  to be lost

I tried various implementations but it appears that by reattaching the data
to the new node by copying the expando property is the most reliable of all.
  • Loading branch information
IgorMinar committed May 4, 2012
1 parent a44d3dc commit b431ee3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
*/

var jqCache = {},
jqName = 'ng-' + new Date().getTime(),
jqName = JQLite.expando = 'ng-' + new Date().getTime(),
jqId = 1,
addEventListenerFn = (window.document.addEventListener
? function(element, type, fn) {element.addEventListener(type, fn, false);}
Expand Down
18 changes: 6 additions & 12 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -867,22 +867,12 @@ function $CompileProvider($provide) {
linkRootElement = linkQueue.pop(),
origLinkNode = linkQueue.pop(),
scope = linkQueue.pop(),
linkNode = compileNode,
$origLinkNode = jqLite(origLinkNode);
linkNode = compileNode;

if (origLinkNode !== origCompileNode) {
// it was cloned therefore we have to clone as well.
linkNode = JQLiteClone(compileNode);
replaceWith(linkRootElement, $origLinkNode, linkNode);
}

if (replace) {
if ($origLinkNode.data('$scope')) {
// if the original element before replacement had a new scope, the replacement should
// get it as well
jqLite(linkNode).data('$scope', scope);
}
dealoc($origLinkNode);
replaceWith(linkRootElement, jqLite(origLinkNode), linkNode);
}

afterTemplateNodeLinkFn(function() {
Expand Down Expand Up @@ -992,12 +982,16 @@ function $CompileProvider($provide) {
for(i = 0, ii = $rootElement.length; i < ii; i++) {
if ($rootElement[i] == oldNode) {
$rootElement[i] = newNode;
break;
}
}
}

if (parent) {
parent.replaceChild(newNode, oldNode);
}

newNode[jqLite.expando] = oldNode[jqLite.expando];
$element[0] = newNode;
}
}];
Expand Down

0 comments on commit b431ee3

Please sign in to comment.