make getter-setters json-serializable

This commit is contained in:
Leo Horie 2014-04-19 22:22:02 -04:00
parent 0aba8aa4bd
commit cb6994dd93
9 changed files with 49 additions and 14 deletions

View file

@ -319,10 +319,14 @@ Mithril = m = new function app(window) {
//model
m.prop = function(store) {
return function() {
var prop = function() {
if (arguments.length) store = arguments[0]
return store
}
prop.toJSON = function() {
return store
}
return prop
}
m.deferred = function() {
@ -998,7 +1002,15 @@ function testMithril(mock) {
test(function() {
var prop = m.prop("test")
prop("foo")
return prop() == "foo"
return prop() === "foo"
})
test(function() {
var prop = m.prop("test")
return JSON.stringify(prop) === '"test"'
})
test(function() {
var obj = {prop: m.prop("test")}
return JSON.stringify(obj) === '{"prop":"test"}'
})
//m.request

View file

@ -319,10 +319,14 @@ Mithril = m = new function app(window) {
//model
m.prop = function(store) {
return function() {
var prop = function() {
if (arguments.length) store = arguments[0]
return store
}
prop.toJSON = function() {
return store
}
return prop
}
m.deferred = function() {

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

View file

@ -61,7 +61,7 @@
</div>
<div class="col(9,9,12)">
<h2 id="m-prop">m.prop</h2>
<p>This is a getter-setter factory utility. It returns a function that stores information</p>
<p>This is a getter-setter factory utility. It returns a function that stores information.</p>
<hr>
<h3 id="usage">Usage</h3>
<pre><code class="lang-javascript">//define a getter-setter with initial value `John`
@ -101,6 +101,12 @@ m.request({method: &quot;GET&quot;, url: &quot;/users&quot;})
//then when resolved (e.g. in a view), the `users` getter-setter will contain a list of User instances
//i.e. users()[0].name() == &quot;John&quot;</code></pre>
<hr>
<h3 id="serializing-getter-setters">Serializing getter-setters</h3>
<p>Getter-setters are JSON-serializable:</p>
<pre><code class="lang-javascript">var data = {foo: m.prop(&quot;bar&quot;)};
JSON.stringify(data); // &#39;{&quot;foo&quot;: &quot;bar&quot;}&#39;</code></pre>
<p>This allows getter-setters to be passed directly as parameters to <a href="mithril.request.html"><code>m.request</code></a>, for example.</p>
<hr>
<h3 id="signature">Signature</h3>
<p><a href="how-to-read-signatures.html">How to read signatures</a></p>
<pre><code class="lang-clike">GetterSetter prop([any initialValue])

View file

@ -1,6 +1,6 @@
## m.prop
This is a getter-setter factory utility. It returns a function that stores information
This is a getter-setter factory utility. It returns a function that stores information.
---
@ -56,6 +56,19 @@ m.request({method: "GET", url: "/users"})
---
### Serializing getter-setters
Getter-setters are JSON-serializable:
```javascript
var data = {foo: m.prop("bar")};
JSON.stringify(data); // '{"foo": "bar"}'
```
This allows getter-setters to be passed directly as parameters to [`m.request`](mithril.request.md), for example.
---
### Signature
[How to read signatures](how-to-read-signatures.md)

View file

@ -319,14 +319,14 @@ Mithril = m = new function app(window) {
//model
m.prop = function(store) {
var f = function() {
var prop = function() {
if (arguments.length) store = arguments[0]
return store
}
f.toJSON = function() {
prop.toJSON = function() {
return store
}
return f
return prop
}
m.deferred = function() {

View file

@ -417,15 +417,15 @@ function testMithril(mock) {
test(function() {
var prop = m.prop("test")
prop("foo")
return prop() == "foo"
return prop() === "foo"
})
test(function() {
var prop = m.prop("test")
return JSON.stringify(prop) == "\"test\""
return JSON.stringify(prop) === '"test"'
})
test(function() {
var obj = { prop: m.prop("test") }
return JSON.stringify(obj) == "{\"prop\":\"test\"}"
var obj = {prop: m.prop("test")}
return JSON.stringify(obj) === '{"prop":"test"}'
})
//m.request