diff --git a/mithril.closure-compiler-externs.js b/mithril.closure-compiler-externs.js index ed3575ca..294c3ee1 100644 --- a/mithril.closure-compiler-externs.js +++ b/mithril.closure-compiler-externs.js @@ -1,5 +1,6 @@ var m = { "render": function () {}, + "mount": function () {}, "trust": function () {}, "module": function () {}, "redraw": function () {}, @@ -11,5 +12,6 @@ var m = { "deferred": function () {}, "sync": function () {}, "request": function () {}, - "deps": function () {} + "deps": function () {}, + "component": function() {} } diff --git a/mithril.js b/mithril.js index d8a8f8b0..87eeb31f 100644 --- a/mithril.js +++ b/mithril.js @@ -22,7 +22,7 @@ var m = (function app(window, undefined) { var $document, $location, $requestAnimationFrame, $cancelAnimationFrame; // self invoking function needed because of the way mocks work - function initialize(window){ + function initialize(window) { $document = window.document; $location = window.location; $cancelAnimationFrame = window.cancelAnimationFrame || window.clearTimeout; @@ -31,9 +31,9 @@ var m = (function app(window, undefined) { initialize(window); - m.version = function(){ - return VERSION; - }; + m.version = function() { + return VERSION; + }; /** * @typedef {String} Tag @@ -624,6 +624,23 @@ var m = (function app(window, undefined) { else if (cached.children.tag) unload(cached.children); } } + + var insertAdjacentBeforeEnd = (function () { + var rangeStrategy = function (parentElement, data) { + parentElement.appendChild($document.createRange().createContextualFragment(data)); + }; + var insertAdjacentStrategy = function (parentElement, data) { + parentElement.insertAdjacentHTML("beforeend", data); + }; + + try { + $document.createRange().createContextualFragment('x'); + return rangeStrategy; + } catch (e) { + return insertAdjacentStrategy; + } + })(); + function injectHTML(parentElement, index, data) { var nextSibling = parentElement.childNodes[index]; if (nextSibling) { @@ -636,7 +653,8 @@ var m = (function app(window, undefined) { } else nextSibling.insertAdjacentHTML("beforebegin", data); } - else parentElement.insertAdjacentHTML("beforeend", data); + else insertAdjacentBeforeEnd(parentElement, data); + var nodes = []; while (parentElement.childNodes[index] !== nextSibling) { nodes.push(parentElement.childNodes[index]); @@ -764,6 +782,8 @@ var m = (function app(window, undefined) { controllers[index].onunload(event); } + var isNullComponent = component === null; + if (!isPrevented) { m.redraw.strategy("all"); m.startComputation(); @@ -777,14 +797,24 @@ var m = (function app(window, undefined) { components[index] = component; } endFirstComputation(); + if (isNullComponent) { + removeRootElement(root, index); + } return controllers[index]; } - if (!component) { - roots.splice(index, 1); - controllers.splice(index, 1); - components.splice(index, 1); + if (isNullComponent) { + removeRootElement(root, index); } }; + + function removeRootElement(root, index) { + roots.splice(index, 1); + controllers.splice(index, 1); + components.splice(index, 1); + reset(root); + nodeCache.splice(getCellCacheKey(root), 1); + } + var redrawing = false, forcing = false; m.redraw = function(force) { if (redrawing) return; @@ -851,11 +881,12 @@ var m = (function app(window, undefined) { else m.endComputation(); } - m.withAttr = function(prop, withAttrCallback) { + m.withAttr = function(prop, withAttrCallback, callbackThis) { return function(e) { e = e || event; var currentTarget = e.currentTarget || this; - withAttrCallback(prop in currentTarget ? currentTarget[prop] : currentTarget.getAttribute(prop)); + var _this = callbackThis || this; + withAttrCallback.call(_this, prop in currentTarget ? currentTarget[prop] : currentTarget.getAttribute(prop)); }; }; @@ -1055,6 +1086,14 @@ var m = (function app(window, undefined) { return propify(promise.then(resolve, reject), initialValue); }; prop["catch"] = prop.then.bind(null, null); + prop["finally"] = function(callback){ + var _callback = function(value){return m.deferred().resolve(callback(value)).promise;}; + return prop.then(function(value) { + return propify(_callback(value).then(function() {return value;}), initialValue); + }, function(reason) { + return propify(_callback(reason).then(function() {throw new Error(reason);}), initialValue); + }); + }; return prop; } //Promiz.mithril.js | Zolmeister | MIT diff --git a/package.json b/package.json index 819228f7..ffd7ac2e 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,11 @@ { "name": "mithril", "description": "Mithril.js beta build - use this to help us test the releases before they are released", - "version": "0.1.34-beta.0", - "repository": { - "type": "git", - "url": "git@github.com:lhorie/mithril.js.git" - }, + "version": "0.2.1", + "repository": { + "type": "git", + "url": "git@github.com:lhorie/mithril.js.git" + }, "scripts": { "test": "grunt test" }, diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index d0fd3dad..f1597309 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -1380,6 +1380,13 @@ function testMithril(mock) { handler({currentTarget: {test: "foo"}}) return value === "foo" }) + test(function() { + var value + var _this + var handler = m.withAttr("test", function(data) {value = data}, _this) + handler({currentTarget: {test: "foo"}}) + return value === "foo" && handler.this === _this + }) //m.trust test(function() {return m.trust("test").valueOf() === "test"}) @@ -3915,6 +3922,35 @@ function testMithril(mock) { mock.XMLHttpRequest.$instances.pop().onreadystatechange() return prop().message === "error occurred" && error().message === "error occurred" }) + test(function() { + // Data returned by then() functions propagate to finally(). + var data = m.prop(""); + var prop = m.request({method: "GET", url: "test"}).then(function() {return "foo"})["finally"](data) + mock.XMLHttpRequest.$instances.pop().onreadystatechange() + return prop() === "foo" && data() === "foo" + }) + test(function() { + // Data returned by finally() functions do *not* propagate to next promise. + var data = m.prop(""); + var prop = m.request({method: "GET", url: "test"}).then(function() {return "foo"})["finally"](function() {return "bar"}).then(data) + mock.XMLHttpRequest.$instances.pop().onreadystatechange() + return prop() === "foo" && data() === "foo" + }) + test(function() { + // Errors thrown in finally() can be caught by next promise. + var error = m.prop("no error") + var prop = m.request({method: "GET", url: "test"}).then(function() {return "foo"})["finally"](function() {throw new Error("error occurred")})["catch"](error) + mock.XMLHttpRequest.$instances.pop().onreadystatechange() + return prop().message === "error occurred" && error().message === "error occurred" + }) + test(function() { + // Promise finally() method occurrs after catch(). + var error = m.prop("no error") + var lastly = function() { error("lastly") }; + var prop = m.request({method: "GET", url: "test", deserialize: function() {throw new Error("error occurred")}})["catch"](error)["finally"](lastly) + mock.XMLHttpRequest.$instances.pop().onreadystatechange() + return prop().message === "error occurred" && error() === "lastly" + }) test(function() { var error = m.prop("no error"), exception var prop = m.request({method: "GET", url: "test", deserialize: function() {throw new TypeError("error occurred")}}).then(null, error)