From 8e781592fda77848abd043435623cdbe43d7977f Mon Sep 17 00:00:00 2001 From: Benjamin White Date: Wed, 25 Jun 2014 16:53:07 +0100 Subject: [PATCH 1/2] Added unit test to demonstrate failure of history when navigating very quickly after first router initialisation. --- test/browser/html5-routes-test.js | 33 +++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/browser/html5-routes-test.js b/test/browser/html5-routes-test.js index 628d3c9..760a26b 100644 --- a/test/browser/html5-routes-test.js +++ b/test/browser/html5-routes-test.js @@ -1,5 +1,38 @@ var browser_history_support = (window.history != null ? window.history.pushState : null) != null; +// This test doesn't use the createTest since createTest runs init on the router before +// running the test, which is partially what we want to test. Additionally this needs to be the +// first test in the file as onpopstate will have been remapped otherwise, preventing the fail +// demonstrated by first router initialization on page load +asyncTest('fire the correct route when initializing the router', function(){ + var router = new Router({ + on: function(){ + clearTimeout(t); + ok(true); + finalize(); + } + }), + finalize = function () { + router.destroy(); + start(); + }, + t = setTimeout(function () { + ok(false, 'route was not fired on initial navigation'); + finalize(); + }, 1000); + router.configure({ + html5history: true, + run_handler_in_init: false + }); + router.init(); + try { + router.setRoute('/'); + } + catch(e){ + ok(false,'Exception thrown: ' + e.toString()); + } +}); + createTest('Nested route with the many children as a tokens, callbacks should yield historic params', { '/a': { '/:id': { From f8def474246130852f8e7075d1d2ab3606d4d05a Mon Sep 17 00:00:00 2001 From: Benjamin White Date: Thu, 26 Jun 2014 10:41:57 +0100 Subject: [PATCH 2/2] Fixed to use correct event names for different browsers --- build/director.js | 9 ++++++--- build/director.min.js | 4 ++-- lib/director/browser.js | 7 +++++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/build/director.js b/build/director.js index 504896f..c057f21 100644 --- a/build/director.js +++ b/build/director.js @@ -1,7 +1,7 @@ // -// Generated on Tue Apr 01 2014 11:43:26 GMT-0700 (PDT) by Nodejitsu, Inc (Using Codesurgeon). +// Generated on Thu Jun 26 2014 10:43:17 GMT+0100 (BST) by Nodejitsu, Inc (Using Codesurgeon). // Version 1.2.3 // @@ -106,8 +106,11 @@ var listener = { if(document.readyState === 'complete') { onDOMReady(); } - else { - window.addEventListener('onload', onDOMReady); + else if (window.addEventListener) { + window.addEventListener('load', onDOMReady, false); + } + else if (window.attachEvent) { + window.attachEvent('onload', onDOMReady); } } else { diff --git a/build/director.min.js b/build/director.min.js index 4a5e674..1d7f8a8 100644 --- a/build/director.min.js +++ b/build/director.min.js @@ -1,7 +1,7 @@ // -// Generated on Tue Apr 01 2014 11:43:27 GMT-0700 (PDT) by Nodejitsu, Inc (Using Codesurgeon). +// Generated on Thu Jun 26 2014 10:43:17 GMT+0100 (BST) by Nodejitsu, Inc (Using Codesurgeon). // Version 1.2.3 // -(function(a){function k(a,b,c,d){var e=0,f=0,g=0,c=(c||"(").toString(),d=(d||")").toString(),h;for(h=0;hi.indexOf(d,e)||~i.indexOf(c,e)&&!~i.indexOf(d,e)||!~i.indexOf(c,e)&&~i.indexOf(d,e)){f=i.indexOf(c,e),g=i.indexOf(d,e);if(~f&&!~g||!~f&&~g){var j=a.slice(0,(h||1)+1).join(b);a=[j].concat(a.slice((h||1)+1))}e=(g>f?g:f)+1,h=0}else e=0}return a}function j(a,b){var c,d=0,e="";while(c=a.substr(d).match(/[^\w\d\- %@&]*\*[^\w\d\- %@&]*/))d=c.index+c[0].length,c[0]=c[0].replace(/^\*/,"([_.()!\\ %@&a-zA-Z0-9-]+)"),e+=a.substr(0,c.index)+c[0];a=e+=a.substr(d);var f=a.match(/:([^\/]+)/ig),g,h;if(f){h=f.length;for(var j=0;j7)){if(this.history===!0){var f=!1,g=this.fire;this.fire=function(){f=!0};var h=function(){setTimeout(function(){c.fire=g,window.onpopstate=d,f&&c.fire()},1)};document.readyState==="complete"?h():window.addEventListener("onload",h)}else window.onhashchange=d;this.mode="modern"}else{var i=document.createElement("iframe");i.id="state-frame",i.style.display="none",document.body.appendChild(i),this.writeFrame(""),"onpropertychange"in document&&"attachEvent"in document&&document.attachEvent("onpropertychange",function(){event.propertyName==="location"&&c.check()}),window.setInterval(function(){c.check()},50),this.onHashChanged=d,this.mode="legacy"}e.listeners.push(a);return this.mode},destroy:function(a){if(!!e&&!!e.listeners){var b=e.listeners;for(var c=b.length-1;c>=0;c--)b[c]===a&&b.splice(c,1)}},setHash:function(a){this.mode==="legacy"&&this.writeFrame(a),this.history===!0?(window.history.pushState({},document.title,a),this.fire()):b.hash=a[0]==="/"?a:"/"+a;return this},writeFrame:function(a){var b=document.getElementById("state-frame"),c=b.contentDocument||b.contentWindow.document;c.open(),c.write("