Merge remote-tracking branch 'origin/next' into next
This commit is contained in:
commit
5944ed026b
4 changed files with 94 additions and 17 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
var m = {
|
var m = {
|
||||||
"render": function () {},
|
"render": function () {},
|
||||||
|
"mount": function () {},
|
||||||
"trust": function () {},
|
"trust": function () {},
|
||||||
"module": function () {},
|
"module": function () {},
|
||||||
"redraw": function () {},
|
"redraw": function () {},
|
||||||
|
|
@ -11,5 +12,6 @@ var m = {
|
||||||
"deferred": function () {},
|
"deferred": function () {},
|
||||||
"sync": function () {},
|
"sync": function () {},
|
||||||
"request": function () {},
|
"request": function () {},
|
||||||
"deps": function () {}
|
"deps": function () {},
|
||||||
|
"component": function() {}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
61
mithril.js
61
mithril.js
|
|
@ -22,7 +22,7 @@ var m = (function app(window, undefined) {
|
||||||
var $document, $location, $requestAnimationFrame, $cancelAnimationFrame;
|
var $document, $location, $requestAnimationFrame, $cancelAnimationFrame;
|
||||||
|
|
||||||
// self invoking function needed because of the way mocks work
|
// self invoking function needed because of the way mocks work
|
||||||
function initialize(window){
|
function initialize(window) {
|
||||||
$document = window.document;
|
$document = window.document;
|
||||||
$location = window.location;
|
$location = window.location;
|
||||||
$cancelAnimationFrame = window.cancelAnimationFrame || window.clearTimeout;
|
$cancelAnimationFrame = window.cancelAnimationFrame || window.clearTimeout;
|
||||||
|
|
@ -31,9 +31,9 @@ var m = (function app(window, undefined) {
|
||||||
|
|
||||||
initialize(window);
|
initialize(window);
|
||||||
|
|
||||||
m.version = function(){
|
m.version = function() {
|
||||||
return VERSION;
|
return VERSION;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {String} Tag
|
* @typedef {String} Tag
|
||||||
|
|
@ -624,6 +624,23 @@ var m = (function app(window, undefined) {
|
||||||
else if (cached.children.tag) unload(cached.children);
|
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) {
|
function injectHTML(parentElement, index, data) {
|
||||||
var nextSibling = parentElement.childNodes[index];
|
var nextSibling = parentElement.childNodes[index];
|
||||||
if (nextSibling) {
|
if (nextSibling) {
|
||||||
|
|
@ -636,7 +653,8 @@ var m = (function app(window, undefined) {
|
||||||
}
|
}
|
||||||
else nextSibling.insertAdjacentHTML("beforebegin", data);
|
else nextSibling.insertAdjacentHTML("beforebegin", data);
|
||||||
}
|
}
|
||||||
else parentElement.insertAdjacentHTML("beforeend", data);
|
else insertAdjacentBeforeEnd(parentElement, data);
|
||||||
|
|
||||||
var nodes = [];
|
var nodes = [];
|
||||||
while (parentElement.childNodes[index] !== nextSibling) {
|
while (parentElement.childNodes[index] !== nextSibling) {
|
||||||
nodes.push(parentElement.childNodes[index]);
|
nodes.push(parentElement.childNodes[index]);
|
||||||
|
|
@ -764,6 +782,8 @@ var m = (function app(window, undefined) {
|
||||||
controllers[index].onunload(event);
|
controllers[index].onunload(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var isNullComponent = component === null;
|
||||||
|
|
||||||
if (!isPrevented) {
|
if (!isPrevented) {
|
||||||
m.redraw.strategy("all");
|
m.redraw.strategy("all");
|
||||||
m.startComputation();
|
m.startComputation();
|
||||||
|
|
@ -777,14 +797,24 @@ var m = (function app(window, undefined) {
|
||||||
components[index] = component;
|
components[index] = component;
|
||||||
}
|
}
|
||||||
endFirstComputation();
|
endFirstComputation();
|
||||||
|
if (isNullComponent) {
|
||||||
|
removeRootElement(root, index);
|
||||||
|
}
|
||||||
return controllers[index];
|
return controllers[index];
|
||||||
}
|
}
|
||||||
if (!component) {
|
if (isNullComponent) {
|
||||||
roots.splice(index, 1);
|
removeRootElement(root, index);
|
||||||
controllers.splice(index, 1);
|
|
||||||
components.splice(index, 1);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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;
|
var redrawing = false, forcing = false;
|
||||||
m.redraw = function(force) {
|
m.redraw = function(force) {
|
||||||
if (redrawing) return;
|
if (redrawing) return;
|
||||||
|
|
@ -851,11 +881,12 @@ var m = (function app(window, undefined) {
|
||||||
else m.endComputation();
|
else m.endComputation();
|
||||||
}
|
}
|
||||||
|
|
||||||
m.withAttr = function(prop, withAttrCallback) {
|
m.withAttr = function(prop, withAttrCallback, callbackThis) {
|
||||||
return function(e) {
|
return function(e) {
|
||||||
e = e || event;
|
e = e || event;
|
||||||
var currentTarget = e.currentTarget || this;
|
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);
|
return propify(promise.then(resolve, reject), initialValue);
|
||||||
};
|
};
|
||||||
prop["catch"] = prop.then.bind(null, null);
|
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;
|
return prop;
|
||||||
}
|
}
|
||||||
//Promiz.mithril.js | Zolmeister | MIT
|
//Promiz.mithril.js | Zolmeister | MIT
|
||||||
|
|
|
||||||
10
package.json
10
package.json
|
|
@ -1,11 +1,11 @@
|
||||||
{
|
{
|
||||||
"name": "mithril",
|
"name": "mithril",
|
||||||
"description": "Mithril.js beta build - use this to help us test the releases before they are released",
|
"description": "Mithril.js beta build - use this to help us test the releases before they are released",
|
||||||
"version": "0.1.34-beta.0",
|
"version": "0.2.1",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git@github.com:lhorie/mithril.js.git"
|
"url": "git@github.com:lhorie/mithril.js.git"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "grunt test"
|
"test": "grunt test"
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -1380,6 +1380,13 @@ function testMithril(mock) {
|
||||||
handler({currentTarget: {test: "foo"}})
|
handler({currentTarget: {test: "foo"}})
|
||||||
return value === "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
|
//m.trust
|
||||||
test(function() {return m.trust("test").valueOf() === "test"})
|
test(function() {return m.trust("test").valueOf() === "test"})
|
||||||
|
|
@ -3915,6 +3922,35 @@ function testMithril(mock) {
|
||||||
mock.XMLHttpRequest.$instances.pop().onreadystatechange()
|
mock.XMLHttpRequest.$instances.pop().onreadystatechange()
|
||||||
return prop().message === "error occurred" && error().message === "error occurred"
|
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() {
|
test(function() {
|
||||||
var error = m.prop("no error"), exception
|
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)
|
var prop = m.request({method: "GET", url: "test", deserialize: function() {throw new TypeError("error occurred")}}).then(null, error)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue