From 91a32af76c5325696b97378652002e5f8b6c601a Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Tue, 29 Apr 2014 23:14:09 -0400 Subject: [PATCH] add ability to run requests in background --- archive/v0.1.11/change-log.html | 3 +- archive/v0.1.11/mithril-tests.js | 53 +++++++++++++++++++++------ archive/v0.1.11/mithril.js | 22 +++++------ archive/v0.1.11/mithril.min.js | 2 +- archive/v0.1.11/mithril.min.map | 2 +- archive/v0.1.11/mithril.min.zip | Bin 40693 -> 40930 bytes archive/v0.1.11/mithril.redraw.html | 2 +- archive/v0.1.11/mithril.request.html | 8 ++++ docs/change-log.md | 3 +- docs/mithril.redraw.md | 2 +- docs/mithril.request.md | 14 +++++++ mithril.js | 22 +++++------ tests/mithril-tests.js | 31 +++++++++++++++- 13 files changed, 123 insertions(+), 41 deletions(-) diff --git a/archive/v0.1.11/change-log.html b/archive/v0.1.11/change-log.html index ff3d3053..de6da58d 100644 --- a/archive/v0.1.11/change-log.html +++ b/archive/v0.1.11/change-log.html @@ -62,10 +62,11 @@

Change Log

-

v0.1.11 - maintenance

+

v0.1.11 - enhancement

News:


v0.1.10 - maintenance

diff --git a/archive/v0.1.11/mithril-tests.js b/archive/v0.1.11/mithril-tests.js index 7cc8ad61..ba71f23e 100644 --- a/archive/v0.1.11/mithril-tests.js +++ b/archive/v0.1.11/mithril-tests.js @@ -222,28 +222,28 @@ Mithril = m = new function app(window) { m.endComputation() } m.redraw = function() { - for (var i = 0; i < roots.length; i++) { - m.render(roots[i], modules[i].view(controllers[i])) - } - lastRedraw = now - } - function redraw() { now = window.performance && window.performance.now ? window.performance.now() : new window.Date().getTime() - if (now - lastRedraw > 16) m.redraw() + if (now - lastRedraw > 16) redraw() else { var cancel = window.cancelAnimationFrame || window.clearTimeout var defer = window.requestAnimationFrame || window.setTimeout cancel(lastRedrawId) - lastRedrawId = defer(m.redraw, 0) + lastRedrawId = defer(redraw, 0) } } + function redraw() { + for (var i = 0; i < roots.length; i++) { + m.render(roots[i], modules[i].view(controllers[i])) + } + lastRedraw = now + } var pendingRequests = 0, computePostRedrawHook = null m.startComputation = function() {pendingRequests++} m.endComputation = function() { pendingRequests = Math.max(pendingRequests - 1, 0) if (pendingRequests == 0) { - redraw() + m.redraw() if (computePostRedrawHook) { computePostRedrawHook() computePostRedrawHook = null @@ -437,7 +437,7 @@ Mithril = m = new function app(window) { } m.request = function(xhrOptions) { - m.startComputation() + if (xhrOptions.background !== true) m.startComputation() var deferred = m.deferred() var serialize = xhrOptions.serialize || JSON.stringify var deserialize = xhrOptions.deserialize || JSON.parse @@ -455,7 +455,7 @@ Mithril = m = new function app(window) { else if (xhrOptions.type) response = new xhrOptions.type(response) deferred.promise(response) deferred[e.type == "load" ? "resolve" : "reject"](response) - m.endComputation() + if (xhrOptions.background !== true) m.endComputation() } ajax(xhrOptions) deferred.promise.then = propBinder(deferred.promise) @@ -924,10 +924,17 @@ function testMithril(mock) { m.render(root, ["bar"]) return root.childNodes.length == 1 }) + test(function() { + //https://github.com/lhorie/mithril.js/issues/56 + var root = mock.document.createElement("div") + m.render(root, m("div", "foo")) + return root.childNodes.length == 1 + }) //end m.render //m.redraw test(function() { + mock.performance.$elapse(50) var controller var root = mock.document.createElement("div") m.module(root, { @@ -936,7 +943,29 @@ function testMithril(mock) { }) controller.value = "foo" m.redraw() - return root.childNodes[0].nodeValue === "foo" + var lengthBefore = root.childNodes.length + mock.performance.$elapse(50) + m.redraw() + mock.performance.$elapse(50) + return lengthBefore === 0 && root.childNodes[0].nodeValue === "foo" + }) + test(function() { + mock.performance.$elapse(50) + var count = 0 + var root = mock.document.createElement("div") + m.module(root, { + controller: function() {}, + view: function(ctrl) { + count++ + } + }) + m.redraw() + m.redraw() + m.redraw() + mock.performance.$elapse(50) + m.redraw() + mock.performance.$elapse(50) + return count === 2 }) //m.route diff --git a/archive/v0.1.11/mithril.js b/archive/v0.1.11/mithril.js index 2cdd0c47..06685a0f 100644 --- a/archive/v0.1.11/mithril.js +++ b/archive/v0.1.11/mithril.js @@ -222,28 +222,28 @@ Mithril = m = new function app(window) { m.endComputation() } m.redraw = function() { - for (var i = 0; i < roots.length; i++) { - m.render(roots[i], modules[i].view(controllers[i])) - } - lastRedraw = now - } - function redraw() { now = window.performance && window.performance.now ? window.performance.now() : new window.Date().getTime() - if (now - lastRedraw > 16) m.redraw() + if (now - lastRedraw > 16) redraw() else { var cancel = window.cancelAnimationFrame || window.clearTimeout var defer = window.requestAnimationFrame || window.setTimeout cancel(lastRedrawId) - lastRedrawId = defer(m.redraw, 0) + lastRedrawId = defer(redraw, 0) } } + function redraw() { + for (var i = 0; i < roots.length; i++) { + m.render(roots[i], modules[i].view(controllers[i])) + } + lastRedraw = now + } var pendingRequests = 0, computePostRedrawHook = null m.startComputation = function() {pendingRequests++} m.endComputation = function() { pendingRequests = Math.max(pendingRequests - 1, 0) if (pendingRequests == 0) { - redraw() + m.redraw() if (computePostRedrawHook) { computePostRedrawHook() computePostRedrawHook = null @@ -437,7 +437,7 @@ Mithril = m = new function app(window) { } m.request = function(xhrOptions) { - m.startComputation() + if (xhrOptions.background !== true) m.startComputation() var deferred = m.deferred() var serialize = xhrOptions.serialize || JSON.stringify var deserialize = xhrOptions.deserialize || JSON.parse @@ -455,7 +455,7 @@ Mithril = m = new function app(window) { else if (xhrOptions.type) response = new xhrOptions.type(response) deferred.promise(response) deferred[e.type == "load" ? "resolve" : "reject"](response) - m.endComputation() + if (xhrOptions.background !== true) m.endComputation() } ajax(xhrOptions) deferred.promise.then = propBinder(deferred.promise) diff --git a/archive/v0.1.11/mithril.min.js b/archive/v0.1.11/mithril.min.js index bf20202f..133dc33f 100644 --- a/archive/v0.1.11/mithril.min.js +++ b/archive/v0.1.11/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,l){if(null===h||void 0===h)return void(i&&e(i.nodes));if("retain"!==h.subtree){var m=u.call(i),n=u.call(h);if(m!=n&&(null!==i&&void 0!==i&&e(i.nodes),i=new h.constructor,i.nodes=[]),"[object Array]"==n){for(var o=[],p=i.length===h.length,q=0,r=0,s=0;r-1?new h.constructor(h):h,i.nodes=[v];else if(i.valueOf()!==h.valueOf()||j===!0){if(h.$trusted){var x=i.nodes[0],o=[x];if(x){for(;x=x.nextSibling;)o.push(x);e(o),v=f(b,k,h)}else b.innerHTML=h}else v=i.nodes[0],"textarea"===g?b.value=h:b.insertBefore(v,b.childNodes[k]||null),v.nodeValue=h;i=new h.constructor(h),i.nodes=[v]}else i.nodes.intact=!0}return i}}function d(b,c,d,e,f){for(var g in d){var i=d[g],j=e[g];if(!(g in e)||j!==i||b===a.document.activeElement){if(e[g]=i,"config"===g)continue;if("function"==typeof i&&0==g.indexOf("on"))b[g]=h(i,b);else if("style"===g)for(var k in i)(void 0===j||j[k]!==i[k])&&(b.style[k]=i[k]);else void 0!==f?"href"===g?b.setAttributeNS("http://www.w3.org/1999/xlink","href",i):"className"===g?b.setAttribute("class",i):b.setAttribute(g,i):"value"===g&&"input"===c?b.value!==i&&(b.value=i):g in b?b[g]=i:b.setAttribute(g,i)}}return e}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){L={};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,null,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,l){if(null===h||void 0===h)return void(i&&e(i.nodes));if("retain"!==h.subtree){var m=u.call(i),n=u.call(h);if(m!=n&&(null!==i&&void 0!==i&&e(i.nodes),i=new h.constructor,i.nodes=[]),"[object Array]"==n){for(var o=[],p=i.length===h.length,q=0,r=0,s=0;r-1?new h.constructor(h):h,i.nodes=[v];else if(i.valueOf()!==h.valueOf()||j===!0){if(h.$trusted){var x=i.nodes[0],o=[x];if(x){for(;x=x.nextSibling;)o.push(x);e(o),v=f(b,k,h)}else b.innerHTML=h}else v=i.nodes[0],"textarea"===g?b.value=h:b.insertBefore(v,b.childNodes[k]||null),v.nodeValue=h;i=new h.constructor(h),i.nodes=[v]}else i.nodes.intact=!0}return i}}function d(b,c,d,e,f){for(var g in d){var i=d[g],j=e[g];if(!(g in e)||j!==i||b===a.document.activeElement){if(e[g]=i,"config"===g)continue;if("function"==typeof i&&0==g.indexOf("on"))b[g]=h(i,b);else if("style"===g)for(var k in i)(void 0===j||j[k]!==i[k])&&(b.style[k]=i[k]);else void 0!==f?"href"===g?b.setAttributeNS("http://www.w3.org/1999/xlink","href",i):"className"===g?b.setAttribute("class",i):b.setAttribute(g,i):"value"===g&&"input"===c?b.value!==i&&(b.value=i):g in b?b[g]=i:b.setAttribute(g,i)}}return e}function e(a){for(var b=0;b0&&("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,null,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(){if(D=a.performance&&a.performance.now?a.performance.now():(new a.Date).getTime(),D-E>16)i();else{var b=a.cancelAnimationFrame||a.clearTimeout,c=a.requestAnimationFrame||a.setTimeout;b(F),F=c(i,0)}};var G=0,H=null;b.startComputation=function(){G++},b.endComputation=function(){G=Math.max(G-1,0),0==G&&(b.redraw(),H&&(H(),H=null))},b.withAttr=function(a,b){return function(c){b(a in c.currentTarget?c.currentTarget[a]:c.currentTarget.getAttribute(a))}};var I,J={pathname:"",hash:"#",search:"?"},K=function(){},L={};return b.route=function(){if(0===arguments.length)return I;if(3===arguments.length){I=a.location[b.route.mode].slice(J[b.route.mode].length);var c=arguments[0],d=arguments[1],e=arguments[2];K=function(a){var f=a.slice(J[b.route.mode].length);j(c,e,f)||b.route(d,!0)};var f="hash"==b.route.mode?"onhashchange":"onpopstate";a[f]=function(){K(a.location[b.route.mode])},H=l,a[f]()}else if(arguments[0].addEventListener){var g=arguments[0],h=arguments[1];h||(g.removeEventListener("click",k),g.addEventListener("click",k))}else if("string"==typeof arguments[0]){I=arguments[0];var i=arguments[1]===!0;a.history.pushState?(H=function(){a.history[i?"replaceState":"pushState"](null,a.document.title,J[b.route.mode]+I),l()},K(J[b.route.mode]+I)):a.location[b.route.mode]=I}},b.route.param=function(a){return L[a]},b.route.mode="search",b.prop=function(a){var b=function(){return arguments.length&&(a=arguments[0]),a};return b.toJSON=function(){return a},b},b.deferred=function(){var a=[],c=[],d={resolve:function(b){for(var c=0;c6}H{9wTtXFG^yJrZ8y%taXs-feihPo=I-q5cy>G=MEiqZ{GkPjKUM#rszM+k_|CbTIEGY^ zNHf0Y+HJ$OPs*>}8iT`u+w_N`rv3~7-o>lZWH z*wBG|yFb&tHWH*af>fhrKluFJ_oqhIQtP>`pj0bvoVjx4%-Dqs@^t@0t#=;#OXuyF zt=G>y)Y(T?y<0f_?uFTojtjG`_Xe&VdpanUs+9q4AV?QC(gFS`ls0PlG?D6=bOW#T zQn4D8^7(W?#C)n!U1CeHD3%(ncLv|-db}FcDpe5T)gV*cIo?j5AMM)NJ7FBv9YKeZ)v#vk=61Noyc7|)%njBcR76FBtct7f`6SiTK{2y}xris&i09KVca8qG*$sDGX_<&>Xv3q2Jm{J-9 z0b`zI*sKwI@V6hKg|7)+)YZNTHrpkkdYoe%2NVD!l@k7&$ue%9>}l|Bm@jwC6Zh6A zUwX36FK5c! zMmb+oW|Wy_7ujf6Tj$^)2zHHQw*vc3UH&8TNY9u>YP`{?3|ajrOMEF@mucg?uy$n3 z8hk`8>^0&znW(m+i%A2RMev=B{IZpGitS7gjB5P1!#VJS2syOEFeQ8;Tes}tthiO$ zW9BWOIIP#C8NKA@Dc3@e@!`fij*HbfoAk%En$Y!zaA=Xzh7COzn+uPTYa%*lY6U~r zO9ty3<`7$X`C9bk0(^yHx5yi-25?2L1LkyrCM>*=%~1W`S>C$yi`fGZaiQYD{xb9mEWxk}M@}%rQ1@sKb^Vo4H6j>SA*KR%2!@ zEd?W%h~^$;se@J!-p8J%uCKGDS4m3Ms3=?_?@BQN*GS@;nil*+7PbrlFGSg>J2;`6 z(_B^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Tq`-BqJ>~ zeIVuY;d}QxyQ5>Prye%rWWQ(th>ymflv{z`*H*U%uYj*8CSaU0c#~S7EwAIVA%!Ul z%0LWQF@#0kiMhz*RrqZ^zINus9HZaOGe39}CS?nHb1m^%bSg|%BvYaAcl58<#!!@Z z*PfZj7HaO|)&iu`ZO#+dG1al=m@Nz(c52=ORc^~0kSjNnXEXSWDDoqxs zv&c1WLU+Eu1wu`jtuh7yf^?UV0DHNk|CQLf5B-3pI1T%%tmxeO8Bq>#ynMw1ovgD! zfSrv2!Kl!+Dp8BK#+`=$wnFB%MrzV)llr&oD~B-75V*Ue&n8Ewp)xMUp{$?gon*6@xilH)3+dD(6V!+&{qiU&b zQryQbW5j2oNuqBF;S50#8ZI1nUQ>4fS@9_hVut>gC!dJKuliQx0W4t~_xGZz(Nir? z>bI|-KY@E7oJ}-%4aK9o-Fj0$_Vk&DTmI;+gTGLmdhVs$N7cZ<(cUvp|KxG4pZn^u zt!8bFZxpN z{%<^aw|@B9NA&F%-yErB8qHeybSGECp?bAau4mRVoks6h-+FDR_u@;phlUUQLZWA0 zo_OZqFBt;^$M~0&y^pu>OG+u<$kp=2%cXqzaw%P1aR-MU9N7QC

m.redraw

Redraws the view for the currently active module. Use m.module() to activate a module.

-

This method is called internally by Mithril's auto-redrawing system and is only documented for completeness; you should avoid calling it manually unless you explicitly want a multi-pass redraw cycle.

+

This method is called internally by Mithril's auto-redrawing system and is only documented for completeness; usually you should avoid calling it manually unless you explicitly want a multi-pass redraw cycle. One case where m.redraw may be useful is to force a manual redraw after background requests (see the background option in m.request.

A multi-pass redraw cycle is usually only useful if you need non-trivial UI metrics measurements. A multi-pass cycle may span multiple browser repaints and therefore could cause flash of unbehaviored content (FOUC) and performance degradation.

By default, if you're using either m.route or m.module, m.redraw() is called automatically by Mithril's auto-redrawing system once the controller finishes executing.

m.redraw is also called automatically on event handlers defined in virtual elements.

diff --git a/archive/v0.1.11/mithril.request.html b/archive/v0.1.11/mithril.request.html index 44adddc4..0101c17d 100644 --- a/archive/v0.1.11/mithril.request.html +++ b/archive/v0.1.11/mithril.request.html @@ -255,6 +255,7 @@ where: [String user,] [String password,] [Object<any> data,] + [Boolean background,] [any unwrapSuccess(any data),] [any unwrapError(any data),] [String serialize(any dataToSerialize),] @@ -285,6 +286,13 @@ where:
  • Object data (optional)

    Data to be sent. It's automatically placed in the appropriate section of the request with the appropriate serialization based on method

  • +
  • Boolean background (optional)

    +

    Determines whether the m.request can affect template rendering. Defaults to false.

    +

    If this option is set to true, then the request does NOT call m.startComputation / m.endComputation, and therefore the completion of the request does not trigger an update of the view, even if data has been changed. This option is useful for running operations in the background (i.e. without user intervention).

    +

    In order to force a redraw after a background request, use m.redraw

    +
    m.request({method: "GET", url: "/foo", background: true})
    +  .then(m.redraw); //force redraw
    +
  • any unwrapSuccess(any data) (optional)

    A preprocessor function to unwrap the data from a success response in case the response contains metadata wrapping the data.

    The default value (if this parameter is falsy) is the identity function function(value) {return value}

    diff --git a/docs/change-log.md b/docs/change-log.md index cdeab3f7..443f5a19 100644 --- a/docs/change-log.md +++ b/docs/change-log.md @@ -1,10 +1,11 @@ ## Change Log -[v0.1.11](/mithril/archive/v0.1.11) - maintenance +[v0.1.11](/mithril/archive/v0.1.11) - enhancement ### News: - Added `m.route()` overload to allow reading of current route [#61](https://github.com/lhorie/mithril.js/issues/61) +- Added `background` option to `m.request` to allow requests that don't affect rendering [#62](https://github.com/lhorie/mithril.js/issues/62) --- diff --git a/docs/mithril.redraw.md b/docs/mithril.redraw.md index 7f775845..96667f4a 100644 --- a/docs/mithril.redraw.md +++ b/docs/mithril.redraw.md @@ -2,7 +2,7 @@ Redraws the view for the currently active module. Use [`m.module()`](mithril.module) to activate a module. -This method is called internally by Mithril's auto-redrawing system and is only documented for completeness; you should avoid calling it manually unless you explicitly want a multi-pass redraw cycle. +This method is called internally by Mithril's auto-redrawing system and is only documented for completeness; usually you should avoid calling it manually unless you explicitly want a multi-pass redraw cycle. One case where `m.redraw` may be useful is to force a manual redraw after background requests (see the `background` option in [`m.request`](mithril.request.md). A multi-pass redraw cycle is usually only useful if you need non-trivial UI metrics measurements. A multi-pass cycle may span multiple browser repaints and therefore could cause flash of unbehaviored content (FOUC) and performance degradation. diff --git a/docs/mithril.request.md b/docs/mithril.request.md index 4f51895e..1428e1c2 100644 --- a/docs/mithril.request.md +++ b/docs/mithril.request.md @@ -290,6 +290,7 @@ where: [String user,] [String password,] [Object data,] + [Boolean background,] [any unwrapSuccess(any data),] [any unwrapError(any data),] [String serialize(any dataToSerialize),] @@ -328,6 +329,19 @@ where: Data to be sent. It's automatically placed in the appropriate section of the request with the appropriate serialization based on `method` + - **Boolean background** (optional) + + Determines whether the `m.request` can affect template rendering. Defaults to false. + + If this option is set to true, then the request does NOT call [`m.startComputation` / `m.endComputation`](mithril.computation.md), and therefore the completion of the request does not trigger an update of the view, even if data has been changed. This option is useful for running operations in the background (i.e. without user intervention). + + In order to force a redraw after a background request, use [`m.redraw`](mithril.redraw.md) + + ```javascript + m.request({method: "GET", url: "/foo", background: true}) + .then(m.redraw); //force redraw + ``` + - **any unwrapSuccess(any data)** (optional) A preprocessor function to unwrap the data from a success response in case the response contains metadata wrapping the data. diff --git a/mithril.js b/mithril.js index 2cdd0c47..06685a0f 100644 --- a/mithril.js +++ b/mithril.js @@ -222,28 +222,28 @@ Mithril = m = new function app(window) { m.endComputation() } m.redraw = function() { - for (var i = 0; i < roots.length; i++) { - m.render(roots[i], modules[i].view(controllers[i])) - } - lastRedraw = now - } - function redraw() { now = window.performance && window.performance.now ? window.performance.now() : new window.Date().getTime() - if (now - lastRedraw > 16) m.redraw() + if (now - lastRedraw > 16) redraw() else { var cancel = window.cancelAnimationFrame || window.clearTimeout var defer = window.requestAnimationFrame || window.setTimeout cancel(lastRedrawId) - lastRedrawId = defer(m.redraw, 0) + lastRedrawId = defer(redraw, 0) } } + function redraw() { + for (var i = 0; i < roots.length; i++) { + m.render(roots[i], modules[i].view(controllers[i])) + } + lastRedraw = now + } var pendingRequests = 0, computePostRedrawHook = null m.startComputation = function() {pendingRequests++} m.endComputation = function() { pendingRequests = Math.max(pendingRequests - 1, 0) if (pendingRequests == 0) { - redraw() + m.redraw() if (computePostRedrawHook) { computePostRedrawHook() computePostRedrawHook = null @@ -437,7 +437,7 @@ Mithril = m = new function app(window) { } m.request = function(xhrOptions) { - m.startComputation() + if (xhrOptions.background !== true) m.startComputation() var deferred = m.deferred() var serialize = xhrOptions.serialize || JSON.stringify var deserialize = xhrOptions.deserialize || JSON.parse @@ -455,7 +455,7 @@ Mithril = m = new function app(window) { else if (xhrOptions.type) response = new xhrOptions.type(response) deferred.promise(response) deferred[e.type == "load" ? "resolve" : "reject"](response) - m.endComputation() + if (xhrOptions.background !== true) m.endComputation() } ajax(xhrOptions) deferred.promise.then = propBinder(deferred.promise) diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index 72eb309e..7b95b041 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -320,10 +320,17 @@ function testMithril(mock) { m.render(root, ["bar"]) return root.childNodes.length == 1 }) + test(function() { + //https://github.com/lhorie/mithril.js/issues/56 + var root = mock.document.createElement("div") + m.render(root, m("div", "foo")) + return root.childNodes.length == 1 + }) //end m.render //m.redraw test(function() { + mock.performance.$elapse(50) var controller var root = mock.document.createElement("div") m.module(root, { @@ -332,7 +339,29 @@ function testMithril(mock) { }) controller.value = "foo" m.redraw() - return root.childNodes[0].nodeValue === "foo" + var lengthBefore = root.childNodes.length + mock.performance.$elapse(50) + m.redraw() + mock.performance.$elapse(50) + return lengthBefore === 0 && root.childNodes[0].nodeValue === "foo" + }) + test(function() { + mock.performance.$elapse(50) + var count = 0 + var root = mock.document.createElement("div") + m.module(root, { + controller: function() {}, + view: function(ctrl) { + count++ + } + }) + m.redraw() + m.redraw() + m.redraw() + mock.performance.$elapse(50) + m.redraw() + mock.performance.$elapse(50) + return count === 2 }) //m.route