Skip to content

Commit

Permalink
Added ability to clone data when cloning nodes, and destoy data when …
Browse files Browse the repository at this point in the history
…destroying nodes.
  • Loading branch information
Michael Mathews committed Aug 27, 2009
1 parent 4d7a92e commit f9ba4ce
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
6 changes: 6 additions & 0 deletions src/dom/dom.js
Expand Up @@ -2176,10 +2176,14 @@
glow.dom.get("a").destroy();
*/
destroy: function () {
// remove any data attached to this NodeList
this.get("*").push(this).removeData();

this.appendTo(tmpDiv);
// destroy nodes
tmpDiv.innerHTML = "";
// empty the nodelist

Array.prototype.splice.call(this, 0, this.length);
return this;
},
Expand Down Expand Up @@ -2212,6 +2216,8 @@

while (i--) {
ret[i] = this[i].cloneNode(true);
// copy data onto the new clone
glow.dom.get(ret[i]).data(glow.dom.get(this[i]).data());
}

// some browsers (ie) also clone node properties as attributes
Expand Down
51 changes: 31 additions & 20 deletions test/glow/dom/dom.js
Expand Up @@ -2196,7 +2196,9 @@ t.test("glow.dom.NodeList#position", function() {
node.destroy();
});

t.module("glow.dom.NodeList.data");
/*-----------------------------------------------------------------------------*/

t.module("glow.dom.NodeList#data");

t.test("Load DOM", function() {
t.expect(1);
Expand All @@ -2208,7 +2210,7 @@ t.test("Load DOM", function() {
});
});

t.test("glow.dom.NodeList.data Setup", function() {
t.test("glow.dom.NodeList#data Setup", function() {
t.expect(1);

try {
Expand All @@ -2228,26 +2230,23 @@ t.test("glow.dom.NodeList.data Setup", function() {
t.equals(glow.dom.get("#para1").length, 1, "The created node is found.");
});

t.test("glow.dom.NodeList.data API", function() {
t.test("glow.dom.NodeList#data API", function() {
t.expect(2);
var dataTest = glow.dom.get("#dataTest");

t.equals(typeof dataTest.data, "function", "A NodeList instance has a method named 'data'.");
t.equals(typeof dataTest.removeData, "function", "A NodeList instance has a method named 'removeData'.");
});

t.test("glow.dom.NodeList.data data method", function() {
t.expect(6);
t.test("glow.dom.NodeList#data method", function() {
t.expect(5);
var dataTest = glow.dom.get("#dataTest p");

dataTest.data("colour", "red");
t.equals(dataTest.data("colour"), "red", "Can get the data from NodeList.");
t.equals(dataTest.data("colour"), "red", "Can set and get a key:val from NodeList.");

var data = dataTest.data();
t.equals(data.colour, "red", "Can get the entire data object from cache.");

var color = dataTest.data("colour");
t.equals(color, "red", "Can get a value from the cache by name.");
t.equals(data.colour, "red", "Can get the entire data object from NodeList when given no arguments.");

data = glow.dom.get("#para1").data();
t.equals(data.colour, "red", "Can get the same data from different NodeLists that refer to the same DomElements.");
Expand All @@ -2257,10 +2256,10 @@ t.test("glow.dom.NodeList.data data method", function() {
count: 8
});
t.equals(dataTest.data("size"), "grande", "Can set multiple key:vals at once.");
t.equals(dataTest.data("count"), 8, "All multiple key:vals are set.");
t.equals(dataTest.data("count"), 8, "All the multiple key:vals are set.");
});

t.test("glow.dom.NodeList.data removeData method", function() {
t.test("glow.dom.NodeList#removeData method", function() {
t.expect(4);
var dataTest = glow.dom.get("#dataTest p");

Expand All @@ -2273,17 +2272,29 @@ t.test("glow.dom.NodeList.data removeData method", function() {

dataTest.removeData();
data = dataTest.data();
t.equals(data.size, undefined, "can remove all data.");
t.equals(data.size, undefined, "can remove all data at once.");
});

t.test("glow.dom.NodeList.data Destroy", function() {
t.test("glow.dom.NodeList#data Clone", function() {
t.expect(1);

try {
glow.dom.get("#dataTest").destroy();
}
catch (e) {
}
var testData = glow.dom.get("#dataTest");
testData.data("tea", "milky");

t.equals(glow.dom.get("#dataTest").length, 0, "The destroyed node is not found.");
var testClone = glow.dom.get("#dataTest").clone();
t.equals(testClone.data("tea"), "milky", "Cloned nodes have the same data as the nodes they are based on.");
});

t.test("glow.dom.NodeList#data Destroy", function() {
t.expect(2);

var testData = glow.dom.get("#dataTest");
testData.data("tea", "milky");

t.equals(testData.data("tea"), "milky", "Before being destroyed the node's data is defined.");
testData.destroy();

t.equals(testData.data("tea"), undefined, "After being destroyed the node's data is undefined.");
});

/*-----------------------------------------------------------------------------*/

0 comments on commit f9ba4ce

Please sign in to comment.