From b1275cb778913f3581e94c6aa5343e2c179240b9 Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Thu, 3 Jul 2014 22:11:29 -0400 Subject: [PATCH] clear diff cache when routing --- Gruntfile.js | 2 +- mithril.js | 12 ++++++++---- tests/mithril-tests.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 3497a68d..3f2521ad 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -1,6 +1,6 @@ module.exports = function(grunt) { - var version = "0.1.17" + var version = "0.1.18" var inputFolder = "./docs" var tempFolder = "./temp" diff --git a/mithril.js b/mithril.js index bb74230e..ba9fa9c2 100644 --- a/mithril.js +++ b/mithril.js @@ -297,13 +297,13 @@ Mithril = m = new function app(window) { m.render = function(root, cell) { var configs = [] if (!root) throw new Error("Please ensure the DOM element exists before rendering a template into it.") - var id = getCellCacheId(root) + var id = getCellCacheKey(root) var node = root == window.document || root == window.document.documentElement ? documentNode : root if (cellCache[id] === undefined) clear(node.childNodes) cellCache[id] = build(node, null, undefined, undefined, cell, cellCache[id], false, 0, null, undefined, configs) for (var i = 0; i < configs.length; i++) configs[i]() } - function getCellCacheId(element) { + function getCellCacheKey(element) { var index = nodeCache.indexOf(element) return index < 0 ? nodeCache.push(element) - 1 : index } @@ -433,7 +433,9 @@ Mithril = m = new function app(window) { for (var route in router) { if (route == path) { - clear(root.childNodes, cellCache[getCellCacheId(root)]) + var cacheKey = getCellCacheKey(root) + clear(root.childNodes, cellCache[cacheKey]) + cellCache[cacheKey] = undefined m.module(root, router[route]) return true } @@ -441,7 +443,9 @@ Mithril = m = new function app(window) { var matcher = new RegExp("^" + route.replace(/:[^\/]+?\.{3}/g, "(.*?)").replace(/:[^\/]+/g, "([^\\/]+)") + "\/?$") if (matcher.test(path)) { - clear(root.childNodes, cellCache[getCellCacheId(root)]) + var cacheKey = getCellCacheKey(root) + clear(root.childNodes, cellCache[cacheKey]) + cellCache[cacheKey] = undefined path.replace(matcher, function() { var keys = route.match(/:[^\/]+/g) || [] var values = [].slice.call(arguments, 1, -2) diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index c71f75b6..28666706 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -1137,6 +1137,34 @@ function testMithril(mock) { mock.performance.$elapse(50) //teardown return unloaded == 1 }) + test(function() { + mock.performance.$elapse(50) //setup + mock.location.search = "?" + + var root = mock.document.createElement("div") + m.route.mode = "search" + m.route(root, "/foo", { + "/foo": { + controller: function() {}, + view: function() { + return m("div", "foo"); + } + }, + "/bar": { + controller: function() {}, + view: function() { + return m("div", "bar"); + } + }, + }) + mock.performance.$elapse(50) + var foo = root.childNodes[0].childNodes[0].nodeValue; + m.route("/bar") + mock.performance.$elapse(50) //teardown + console.log(root.childNodes) + var bar = root.childNodes[0].childNodes[0].nodeValue; + return (foo === "foo" && bar === "bar") + }) //end m.route //m.prop