diff --git a/archive/v0.1.9/mithril-tests.js b/archive/v0.1.9/mithril-tests.js index d89ea8e2..7d1a45d2 100644 --- a/archive/v0.1.9/mithril-tests.js +++ b/archive/v0.1.9/mithril-tests.js @@ -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 diff --git a/archive/v0.1.9/mithril.js b/archive/v0.1.9/mithril.js index bd55a22d..91aa2e83 100644 --- a/archive/v0.1.9/mithril.js +++ b/archive/v0.1.9/mithril.js @@ -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() { diff --git a/archive/v0.1.9/mithril.min.js b/archive/v0.1.9/mithril.min.js index 362066b5..a8a32e92 100644 --- a/archive/v0.1.9/mithril.min.js +++ b/archive/v0.1.9/mithril.min.js @@ -4,5 +4,5 @@ http://github.com/lhorie/mithril.js (c) Leo Horie License: MIT */ -Mithril=m=new function(a){function b(){var a=arguments,b="[object Object]"==u.call(a[1]),c=b?a[1]:{},d="class"in c?"class":"className",e=t[a[0]];if(void 0===e){t[a[0]]=e={tag:"div",attrs:{}};for(var f,h=[];f=v.exec(a[0]);)if(""==f[1])e.tag=f[2];else if("#"==f[1])e.attrs.id=f[2];else if("."==f[1])h.push(f[2]);else if("["==f[3][0]){var i=w.exec(f[3]);e.attrs[i[1]]=i[3]||!0}h.length>0&&(e.attrs[d]=h.join(" "))}e=g(e),e.attrs=g(e.attrs),e.children=b?a[2]:a[1];for(var j in c)e.attrs[j]=j==d?(e.attrs[j]||"")+" "+c[j]:c[j];return e}function c(b,g,h,i,j,k){if(null===g||void 0===g)return void(h&&e(h.nodes));if("retain"!==g.subtree){var l=u.call(h),m=u.call(g);if(l!=m&&(null!==h&&void 0!==h&&e(h.nodes),h=new g.constructor,h.nodes=[]),"[object Array]"==m){for(var n=[],o=h.length===g.length,p=0,q=0;q-1?new g.constructor(g):g,h.nodes=[s];else if(h.valueOf()!==g.valueOf()||i===!0){if(g.$trusted){var v=h.nodes[0],n=[v];if(v){for(;v=v.nextSibling;)n.push(v);e(n),s=f(b,j,g)}else b.innerHTML=g}else s=h.nodes[0],b.insertBefore(s,b.childNodes[j]||null),s.nodeValue=g;h=new g.constructor(g),h.nodes=[s]}else h.nodes.intact=!0}return h}}function d(b,c,d,e){for(var f in c){var g=c[f],i=d[f];if(!(f in d)||i!==g||b===a.document.activeElement){if(d[f]=g,"config"===f)continue;if("function"==typeof g&&0==f.indexOf("on"))b[f]=h(g,b);else if("style"===f)for(var j in g)(void 0===i||i[j]!==g[j])&&(b.style[j]=g[j]);else void 0!==e?"href"===f?b.setAttributeNS("http://www.w3.org/1999/xlink","href",g):"className"===f?b.setAttribute("class",g):b.setAttribute(f,g):f in b?b[f]=g:b.setAttribute(f,g)}}return d}function e(a){for(var b=0;b16)b.redraw();else{var c=a.cancelAnimationFrame||a.clearTimeout,d=a.requestAnimationFrame||a.setTimeout;c(F),F=d(b.redraw,0)}}function j(a,c,d){K={};for(var e in c){if(e==d)return!void b.module(a,c[e]);var f=new RegExp("^"+e.replace(/:[^\/]+/g,"([^\\/]+)")+"$");if(f.test(d))return!void d.replace(f,function(){for(var d=e.match(/:[^\/]+/g),f=[].slice.call(arguments,1,-2),g=0;g0&&("GET"==a.method?a.url=a.url+(a.url.indexOf("?")<0?"?":"&")+o(b):a.data=c(b)),a}function q(a,b){var c=a.match(/:[a-z]\w+/gi);if(c&&b)for(var d=0;de?y.push(b)-1:e,g=b==a.document||b==a.document.documentElement?x:b;z[f]=c(g,d,z[f],!1,0)},b.trust=function(a){return a=new String(a),a.$trusted=!0,a};var A=[],B=[],C=[],D=0,E=0,F=0;b.module=function(a,c){b.startComputation();var d=A.indexOf(a);0>d&&(d=A.length),A[d]=a,B[d]=c,C[d]=new c.controller,b.endComputation()},b.redraw=function(){for(var a=0;a0&&(e.attrs[d]=h.join(" "))}e=g(e),e.attrs=g(e.attrs),e.children=b?a[2]:a[1];for(var j in c)e.attrs[j]=j==d?(e.attrs[j]||"")+" "+c[j]:c[j];return e}function c(b,g,h,i,j,k){if(null===g||void 0===g)return void(h&&e(h.nodes));if("retain"!==g.subtree){var l=u.call(h),m=u.call(g);if(l!=m&&(null!==h&&void 0!==h&&e(h.nodes),h=new g.constructor,h.nodes=[]),"[object Array]"==m){for(var n=[],o=h.length===g.length,p=0,q=0;q-1?new g.constructor(g):g,h.nodes=[s];else if(h.valueOf()!==g.valueOf()||i===!0){if(g.$trusted){var v=h.nodes[0],n=[v];if(v){for(;v=v.nextSibling;)n.push(v);e(n),s=f(b,j,g)}else b.innerHTML=g}else s=h.nodes[0],b.insertBefore(s,b.childNodes[j]||null),s.nodeValue=g;h=new g.constructor(g),h.nodes=[s]}else h.nodes.intact=!0}return h}}function d(b,c,d,e){for(var f in c){var g=c[f],i=d[f];if(!(f in d)||i!==g||b===a.document.activeElement){if(d[f]=g,"config"===f)continue;if("function"==typeof g&&0==f.indexOf("on"))b[f]=h(g,b);else if("style"===f)for(var j in g)(void 0===i||i[j]!==g[j])&&(b.style[j]=g[j]);else void 0!==e?"href"===f?b.setAttributeNS("http://www.w3.org/1999/xlink","href",g):"className"===f?b.setAttribute("class",g):b.setAttribute(f,g):f in b?b[f]=g:b.setAttribute(f,g)}}return d}function e(a){for(var b=0;b16)b.redraw();else{var c=a.cancelAnimationFrame||a.clearTimeout,d=a.requestAnimationFrame||a.setTimeout;c(F),F=d(b.redraw,0)}}function j(a,c,d){K={};for(var e in c){if(e==d)return!void b.module(a,c[e]);var f=new RegExp("^"+e.replace(/:[^\/]+/g,"([^\\/]+)")+"$");if(f.test(d))return!void d.replace(f,function(){for(var d=e.match(/:[^\/]+/g),f=[].slice.call(arguments,1,-2),g=0;g0&&("GET"==a.method?a.url=a.url+(a.url.indexOf("?")<0?"?":"&")+o(b):a.data=c(b)),a}function q(a,b){var c=a.match(/:[a-z]\w+/gi);if(c&&b)for(var d=0;de?y.push(b)-1:e,g=b==a.document||b==a.document.documentElement?x:b;z[f]=c(g,d,z[f],!1,0)},b.trust=function(a){return a=new String(a),a.$trusted=!0,a};var A=[],B=[],C=[],D=0,E=0,F=0;b.module=function(a,c){b.startComputation();var d=A.indexOf(a);0>d&&(d=A.length),A[d]=a,B[d]=c,C[d]=new c.controller,b.endComputation()},b.redraw=function(){for(var a=0;a

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.


Usage

//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"

+

Serializing getter-setters

+

Getter-setters are JSON-serializable:

+
var data = {foo: m.prop("bar")};
+JSON.stringify(data); // '{"foo": "bar"}'
+

This allows getter-setters to be passed directly as parameters to m.request, for example.

+

Signature

How to read signatures

GetterSetter prop([any initialValue])
diff --git a/docs/mithril.prop.md b/docs/mithril.prop.md
index ec8b58b8..c03ca6c0 100644
--- a/docs/mithril.prop.md
+++ b/docs/mithril.prop.md
@@ -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)
diff --git a/mithril.js b/mithril.js
index 1b681b5c..91aa2e83 100644
--- a/mithril.js
+++ b/mithril.js
@@ -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() {
diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js
index bb0700ee..53f956ea 100644
--- a/tests/mithril-tests.js
+++ b/tests/mithril-tests.js
@@ -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