make getter-setters json-serializable
This commit is contained in:
parent
0aba8aa4bd
commit
cb6994dd93
9 changed files with 49 additions and 14 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
2
archive/v0.1.9/mithril.min.js
vendored
2
archive/v0.1.9/mithril.min.js
vendored
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.
|
|
@ -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: "GET", url: "/users"})
|
|||
//then when resolved (e.g. in a view), the `users` getter-setter will contain a list of User instances
|
||||
//i.e. users()[0].name() == "John"</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("bar")};
|
||||
JSON.stringify(data); // '{"foo": "bar"}'</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])
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue