diff --git a/docs/components.md b/docs/components.md index e98224f8..7aca51a9 100644 --- a/docs/components.md +++ b/docs/components.md @@ -27,7 +27,7 @@ var Contact = function(data) { this.email = m.prop(data.email) } Contact.list = function(data) { - return m.request({method: "GET", url: "/api/contact", data: data}) + return m.request({method: "GET", url: "/api/contact", type: Contact}) } Contact.save = function(data) { return m.request({method: "POST", url: "/api/contact", data: data}) diff --git a/docs/mithril.component.md b/docs/mithril.component.md index b6396d7a..4995a880 100644 --- a/docs/mithril.component.md +++ b/docs/mithril.component.md @@ -217,7 +217,7 @@ Components can be placed anywhere a regular element can. If you have components ```javascript var App = { - ctrl: function() { + controller: function() { return {data: [1, 2, 3]} } view: function(ctrl) { diff --git a/docs/mithril.route.md b/docs/mithril.route.md index 87715abc..f46e3034 100644 --- a/docs/mithril.route.md +++ b/docs/mithril.route.md @@ -164,7 +164,7 @@ m.route(document.body, "/", { }); //re-route to dashboard -m.route("/dashboard"); // logs "unloading home" +m.route("/dashboard"); // logs "unloading home component" ``` This mechanism is useful to clear timers and unsubscribe event handlers. If you have a hierarchy of components, you can recursively call `onunload` on all the components in the tree or use a [pubsub](http://microjs.com/#pubsub) library to unload specific components on demand. diff --git a/mithril.js b/mithril.js index 707d09aa..d5e55d84 100644 --- a/mithril.js +++ b/mithril.js @@ -859,6 +859,7 @@ var m = (function app(window, undefined) { prop.then = function(resolve, reject) { return propify(promise.then(resolve, reject), initialValue) }; + prop.catch = prop.then.bind(null, null) return prop } //Promiz.mithril.js | Zolmeister | MIT diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index 8374bd3e..436b61f7 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -3872,6 +3872,12 @@ function testMithril(mock) { mock.XMLHttpRequest.$instances.pop().onreadystatechange() return prop().message === "error occurred" && error().message === "error occurred" }) + test(function() { + var error = m.prop("no error") + var prop = m.request({method: "GET", url: "test", deserialize: function() {throw new Error("error occurred")}}).catch(error) + mock.XMLHttpRequest.$instances.pop().onreadystatechange() + return prop().message === "error occurred" && error().message === "error occurred" + }) 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) @@ -4052,6 +4058,13 @@ function testMithril(mock) { deferred.reject("test") return value === "test" }) + test(function() { + var value + var deferred = m.deferred() + deferred.promise.catch(function(data) {value = data}) + deferred.reject("test") + return value === "test" + }) test(function() { var value var deferred = m.deferred() @@ -4059,6 +4072,13 @@ function testMithril(mock) { deferred.reject("test") return value === "foo" }) + test(function() { + var value + var deferred = m.deferred() + deferred.promise.catch(function(data) {return "foo"}).then(function(data) {value = data}) + deferred.reject("test") + return value === "foo" + }) test(function() { var value1, value2 var deferred = m.deferred() @@ -4224,6 +4244,13 @@ function testMithril(mock) { deferred1.resolve("test") return value[0] === "test" && value[1] === "foo" }) + test(function() { + var value + var deferred = m.deferred() + m.sync([deferred.promise]).catch(function(data) {value = data}) + deferred.reject("fail") + return value[0] === "fail" + }) test(function() { var value = 1 m.sync([]).then(function() {value = 2})