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
|
//model
|
||||||
m.prop = function(store) {
|
m.prop = function(store) {
|
||||||
return function() {
|
var prop = function() {
|
||||||
if (arguments.length) store = arguments[0]
|
if (arguments.length) store = arguments[0]
|
||||||
return store
|
return store
|
||||||
}
|
}
|
||||||
|
prop.toJSON = function() {
|
||||||
|
return store
|
||||||
|
}
|
||||||
|
return prop
|
||||||
}
|
}
|
||||||
|
|
||||||
m.deferred = function() {
|
m.deferred = function() {
|
||||||
|
|
@ -998,7 +1002,15 @@ function testMithril(mock) {
|
||||||
test(function() {
|
test(function() {
|
||||||
var prop = m.prop("test")
|
var prop = m.prop("test")
|
||||||
prop("foo")
|
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
|
//m.request
|
||||||
|
|
|
||||||
|
|
@ -319,10 +319,14 @@ Mithril = m = new function app(window) {
|
||||||
|
|
||||||
//model
|
//model
|
||||||
m.prop = function(store) {
|
m.prop = function(store) {
|
||||||
return function() {
|
var prop = function() {
|
||||||
if (arguments.length) store = arguments[0]
|
if (arguments.length) store = arguments[0]
|
||||||
return store
|
return store
|
||||||
}
|
}
|
||||||
|
prop.toJSON = function() {
|
||||||
|
return store
|
||||||
|
}
|
||||||
|
return prop
|
||||||
}
|
}
|
||||||
|
|
||||||
m.deferred = function() {
|
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>
|
||||||
<div class="col(9,9,12)">
|
<div class="col(9,9,12)">
|
||||||
<h2 id="m-prop">m.prop</h2>
|
<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>
|
<hr>
|
||||||
<h3 id="usage">Usage</h3>
|
<h3 id="usage">Usage</h3>
|
||||||
<pre><code class="lang-javascript">//define a getter-setter with initial value `John`
|
<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
|
//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>
|
//i.e. users()[0].name() == "John"</code></pre>
|
||||||
<hr>
|
<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>
|
<h3 id="signature">Signature</h3>
|
||||||
<p><a href="how-to-read-signatures.html">How to read signatures</a></p>
|
<p><a href="how-to-read-signatures.html">How to read signatures</a></p>
|
||||||
<pre><code class="lang-clike">GetterSetter prop([any initialValue])
|
<pre><code class="lang-clike">GetterSetter prop([any initialValue])
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
## m.prop
|
## 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
|
### Signature
|
||||||
|
|
||||||
[How to read signatures](how-to-read-signatures.md)
|
[How to read signatures](how-to-read-signatures.md)
|
||||||
|
|
|
||||||
|
|
@ -319,14 +319,14 @@ Mithril = m = new function app(window) {
|
||||||
|
|
||||||
//model
|
//model
|
||||||
m.prop = function(store) {
|
m.prop = function(store) {
|
||||||
var f = function() {
|
var prop = function() {
|
||||||
if (arguments.length) store = arguments[0]
|
if (arguments.length) store = arguments[0]
|
||||||
return store
|
return store
|
||||||
}
|
}
|
||||||
f.toJSON = function() {
|
prop.toJSON = function() {
|
||||||
return store
|
return store
|
||||||
}
|
}
|
||||||
return f
|
return prop
|
||||||
}
|
}
|
||||||
|
|
||||||
m.deferred = function() {
|
m.deferred = function() {
|
||||||
|
|
|
||||||
|
|
@ -417,15 +417,15 @@ function testMithril(mock) {
|
||||||
test(function() {
|
test(function() {
|
||||||
var prop = m.prop("test")
|
var prop = m.prop("test")
|
||||||
prop("foo")
|
prop("foo")
|
||||||
return prop() == "foo"
|
return prop() === "foo"
|
||||||
})
|
})
|
||||||
test(function() {
|
test(function() {
|
||||||
var prop = m.prop("test")
|
var prop = m.prop("test")
|
||||||
return JSON.stringify(prop) == "\"test\""
|
return JSON.stringify(prop) === '"test"'
|
||||||
})
|
})
|
||||||
test(function() {
|
test(function() {
|
||||||
var obj = {prop: m.prop("test")}
|
var obj = {prop: m.prop("test")}
|
||||||
return JSON.stringify(obj) == "{\"prop\":\"test\"}"
|
return JSON.stringify(obj) === '{"prop":"test"}'
|
||||||
})
|
})
|
||||||
|
|
||||||
//m.request
|
//m.request
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue