diff --git a/can-stache-bindings.js b/can-stache-bindings.js index bdd428f..acd45de 100644 --- a/can-stache-bindings.js +++ b/can-stache-bindings.js @@ -318,7 +318,11 @@ var behaviors = { teardown = dataBinding.onTeardown; attributeDisposal = domMutate.onNodeAttributeChange(el, attributeListener); - removedDisposal = domMutate.onNodeRemoval(el, tearItAllDown); + removedDisposal = domMutate.onNodeRemoval(el, function() { + if (el.ownerDocument.contains(el) === false) { + tearItAllDown(); + } + }); }, // ### bindings.behaviors.event // The following section contains code for implementing the can-EVENT attribute. diff --git a/test/colon/basics-test.js b/test/colon/basics-test.js index 7206c54..dfaf1c6 100644 --- a/test/colon/basics-test.js +++ b/test/colon/basics-test.js @@ -3,6 +3,7 @@ var testHelpers = require('../helpers'); var stacheBindings = require('can-stache-bindings'); +var domEvents = require('can-dom-events'); var stache = require('can-stache'); var SimpleMap = require("can-simple-map"); @@ -388,4 +389,30 @@ testHelpers.makeTests("can-stache-bindings - colon - basics", function(name, doc QUnit.equal(input.value, "VALUE", "value should not have been updated"); }); + QUnit.test("bindings still work for moved elements (#460)", function(assert) { + var done = assert.async(); + var map = new SimpleMap({value: "first"}); + var template = stache(""); + var frag = template(map); + var input = frag.firstChild; + + this.fixture.appendChild(frag); + + // Move the input to inside the div + var div = doc.createElement("div"); + this.fixture.appendChild(div); + div.appendChild(input); + + testHelpers.afterMutation(function() { + map.set("value", "second"); + QUnit.equal(input.value, "second", "value should have been updated"); + + input.value = "third"; + domEvents.dispatch(input, "change"); + QUnit.equal(map.get("value"), "third", "map should have been updated"); + + done(); + }); + }); + });