ded / bonzo

library agnostic, extensible DOM utility
Other
1.32k stars 137 forks source link

.replaceWith() doesn't work when replacing a node with itself #125

Closed ColemanGariety closed 10 years ago

ColemanGariety commented 10 years ago

I'm making a Backbone app and using $('main').replaceWith(view.el) to change views.

However, if I navigate to the same view twice, the view disappears.

This is because replaceWith currently works by adding node after this and then removing this. But if this === node, it adds it and immediately removes it because they are the same node.

A better solution is to use the browser's native Node.prototype.replaceChild() method.

Here's my suggestion, does this look okay?

replaceWith: function (node) {
  return bonzo(this[0].parentNode.replaceChild(bonzo(normalize(node))[0], this[0]))
}

It seems to work. If I don't find any problems I'll fork it and submit a pull req.

ded commented 10 years ago

thanks for putting this together. code looks sane to me.