prevent dom recreation if only class value changed

This commit is contained in:
Leo Horie 2015-04-15 22:40:50 -04:00
parent 94d804d850
commit fce5f4b86f
2 changed files with 8 additions and 1 deletions

View file

@ -61,6 +61,7 @@ var m = (function app(window, undefined) {
if (attrs.hasOwnProperty(attrName)) {
if (attrName === classAttrName && attrs[attrName] != null && attrs[attrName] !== "") {
classes.push(attrs[attrName])
cell.attrs[attrName] = "" //create key in correct iteration order
}
else cell.attrs[attrName] = attrs[attrName]
}
@ -259,7 +260,7 @@ var m = (function app(window, undefined) {
var dataAttrKeys = Object.keys(data.attrs)
var hasKeys = dataAttrKeys.length > ("key" in data.attrs ? 1 : 0)
//if an element is different enough from the one in cache, recreate it
if (data.tag != cached.tag || dataAttrKeys.join() != Object.keys(cached.attrs).join() || data.attrs.id != cached.attrs.id || data.attrs.key != cached.attrs.key || (m.redraw.strategy() == "all" && cached.configContext && cached.configContext.retain !== true) || (m.redraw.strategy() == "diff" && cached.configContext && cached.configContext.retain === false)) {
if (data.tag != cached.tag || dataAttrKeys.sort().join() != Object.keys(cached.attrs).sort().join() || data.attrs.id != cached.attrs.id || data.attrs.key != cached.attrs.key || (m.redraw.strategy() == "all" && cached.configContext && cached.configContext.retain !== true) || (m.redraw.strategy() == "diff" && cached.configContext && cached.configContext.retain === false)) {
if (cached.nodes.length) clear(cached.nodes);
if (cached.configContext && typeof cached.configContext.onunload === FUNCTION) cached.configContext.onunload()
if (cached.controllers) {

View file

@ -41,6 +41,12 @@ function testMithril(mock) {
test(function() {return m("div", [1, 2, 3], [4, 5, 6, 7]).children[0].length === 3})
test(function() {return m("div", [1, 2, 3], [4, 5, 6, 7]).children[1].length === 4})
test(function() {return m("div", [1], [2], [3]).children.length === 3})
test(function() {
//class changes shouldn't trigger dom recreation
var v1 = m(".foo", {class: "", onclick: function() {}})
var v2 = m(".foo", {class: "bar", onclick: function() {}})
return Object.keys(v1.attrs).join() === Object.keys(v2.attrs).join()
})
//m.mount
test(function() {