Merge branch 'next'

This commit is contained in:
Leo Horie 2014-08-10 21:38:06 -04:00
commit 999fdb5c89
8 changed files with 60 additions and 15 deletions

View file

@ -137,10 +137,12 @@ dashboard.controller = function() {
dashboard.view = function(ctrl) {
return m("#example", [
new autocompleter.view(ctrl.autocompleter, {onchange: m.withAttr("value", console.log)}),
new autocompleter.view(ctrl.autocompleter, {onchange: m.withAttr("value", log)}),
]);
};
//an FP-friendly console.log
var log = function(value) {console.log(value)}
//initialize

View file

@ -164,3 +164,13 @@ void test(Object { any | void config(DOMElement) } value)
//example of a valid function call
test({ first: "first", config: function(element) { /*do stuff*/ } })
```
### Nullable Types
A question mark `?` after a type denotes that a value can be either of that type or `undefined`.
```clink
XMLHttpRequest? config()
```
In the example above, the `config` function is expected to return either an instance of the XMLHttpRequest object or `undefined`

View file

@ -136,14 +136,19 @@ For the most part, Mithril promises behave as you'd expect a [Promise/A+](http:/
Mithril promises forward a value downstream if a resolution callback returns `undefined`. This allows simpler debugging of promise chains:
```javascript
//a FP-friendly console.log
var log = function(value) {
console.log(value)
}
var data = m.request({method: "GET", url: "/data"})
.then(console.log) //Mithril promises let us debug like this
.then(log) //Mithril promises let us debug like this
.then(doStuff)
var data = m.request({method: "GET", url: "/data"})
.then(function(value) { // Promises/A+ would require us to declare an anonymous function
.then(function(value) {
console.log(value) // here's the debugging snippet
return value // and we need to remember to return the value as well
return value // Promises/A+ requires us to return a value
})
.then(doStuff) // or else `doStuff` will break

View file

@ -139,8 +139,13 @@ As you saw, you can chain operations that act on the response data. Typically th
In the example below, we take advantage of queuing to debug the AJAX response data prior to doing further processing on the user list
```javascript
//a FP-friendly console.log
var log = function(value) {
console.log(value)
}
var users = m.request({method: "GET", url: "/user"})
.then(console.log);
.then(log);
.then(function(users) {
//add one more user to the response
return users.concat({name: "Jane"})

View file

@ -57,11 +57,11 @@ You can use it by adding a reference to your Typescript files. This will allow t
### Internet Explorer Compatibility
Mithril relies on some Ecmascript 5 features, namely: `Array::indexOf` and `Object::keys`, as well as the `JSON` object.
Mithril relies on some Ecmascript 5 features, namely: `Array::indexOf`, `Object::keys` and `Function::bind`, as well as the `JSON` object.
You can use polyfill libraries to support these features in IE7.
- [ES5 Shim](https://github.com/es-shims/es5-shim) or Mozilla.org's [Array::indexOf](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf) and [Object::keys](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys) polyfills
- [ES5 Shim](https://github.com/es-shims/es5-shim) or Mozilla.org's [Array::indexOf](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf), [Object::keys](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys) and [Function::bind](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) polyfills
- [JSON2.js](https://github.com/douglascrockford/JSON-js/blob/master/json2.js)

View file

@ -0,0 +1,15 @@
var m = {
"render": function () {},
"trust": function () {},
"module": function () {},
"redraw": function () {},
"startComputation": function () {},
"endComputation": function () {},
"withAttr": function () {},
"route": function () {},
"prop": function () {},
"deferred": function () {},
"sync": function () {},
"request": function () {},
"deps": function () {}
}

View file

@ -4,7 +4,7 @@ Mithril = m = new function app(window, undefined) {
function m() {
var args = arguments
var hasAttrs = type.call(args[1]) == "[object Object]" && !("tag" in args[1]) && !("subtree" in args[1])
var hasAttrs = args[1] !== undefined && type.call(args[1]) == "[object Object]" && !("tag" in args[1]) && !("subtree" in args[1])
var attrs = hasAttrs ? args[1] : {}
var classAttrName = "class" in attrs ? "class" : "className"
var cell = {tag: "div", attrs: {}}
@ -29,8 +29,7 @@ Mithril = m = new function app(window, undefined) {
return cell
}
function build(parentElement, parentTag, parentCache, parentIndex, data, cached, shouldReattach, index, editable, namespace, configs) {
if (data === undefined) return undefined
if (data === null) data = ""
if (data === undefined || data === null) data = ""
if (data.subtree === "retain") return cached
var cachedType = type.call(cached), dataType = type.call(data)
@ -555,7 +554,7 @@ Mithril = m = new function app(window, undefined) {
else next[method](result !== undefined ? result : value)
}
catch (e) {
if (e instanceof Error && e.constructor !== Error) throw e
if (type.call(e) == "[object Error]" && e.constructor !== Error) throw e
else next.reject(e)
}
}
@ -587,9 +586,13 @@ Mithril = m = new function app(window, undefined) {
var deferred = m.deferred()
var outstanding = args.length
var results = new Array(outstanding)
for (var i = 0; i < args.length; i++) {
args[i].then(synchronizer(i, true), synchronizer(i, false))
if (args.length > 0) {
for (var i = 0; i < args.length; i++) {
args[i].then(synchronizer(i, true), synchronizer(i, false))
}
}
else deferred.resolve()
return deferred.promise
}
function identity(value) {return value}
@ -659,7 +662,7 @@ Mithril = m = new function app(window, undefined) {
}
catch (e) {
if (e instanceof SyntaxError) throw new SyntaxError("Could not parse HTTP response. See http://lhorie.github.io/mithril/mithril.request.html#using-variable-data-formats")
else if (e instanceof Error && e.constructor !== Error) throw e
else if (type.call(e) == "[object Error]" && e.constructor !== Error) throw e
else deferred.reject(e)
}
if (xhrOptions.background !== true) m.endComputation()

View file

@ -1517,6 +1517,11 @@ function testMithril(mock) {
deferred1.resolve("test")
return value[0] === "test" && value[1] === "foo"
})
test(function() {
var value = 1
m.sync([]).then(function() {value = 2})
return value == 2
})
//m.startComputation/m.endComputation
test(function() {
@ -1588,4 +1593,4 @@ if (typeof window != "undefined") {
//mock
testMithril(mock.window);
test.print(function(value){console.log(value)});
test.print(function(value) {console.log(value)})