Permalink
Browse files

Fixing bug with loading errors (404, etc);

Updating demo;
  • Loading branch information...
1 parent a6af828 commit 04c43b5be695a84c3900f699bb9ad38992b56c78 @benplum benplum committed Mar 13, 2014
Showing with 45 additions and 30 deletions.
  1. +1 −1 bower.json
  2. +1 −1 demo/about.php
  3. +1 −1 demo/index.php
  4. +9 −4 demo/js/main.js
  5. +15 −10 jquery.fs.pronto.js
  6. +2 −2 jquery.fs.pronto.min.js
  7. +1 −1 package.json
  8. +1 −1 pronto.jquery.json
  9. +14 −9 src/jquery.fs.pronto.js
View
@@ -1,6 +1,6 @@
{
"name": "Pronto",
- "version": "3.0.9",
+ "version": "3.0.10",
"description": "A jQuery plugin for faster page loads. Part of the formstone library.",
"keywords": [
"javascript",
View
@@ -22,7 +22,7 @@
<script src="http://formstone.it/js/demo.js"></script>
- <script src="../jquery.fs.pronto.min.js"></script>
+ <script src="../src/jquery.fs.pronto.js"></script>
<script src="js/main.js"></script>
<style>
View
@@ -22,7 +22,7 @@
<script src="http://formstone.it/js/demo.js"></script>
- <script src="../jquery.fs.pronto.min.js"></script>
+ <script src="../src/jquery.fs.pronto.js"></script>
<script src="js/main.js"></script>
<style>
View
@@ -2,7 +2,8 @@
// Bind pronto events
$(window).on("pronto.request", requestPage)
.on("pronto.load", destroyPage)
- .on("pronto.render", initPage);
+ .on("pronto.render", initPage)
+ .on("pronto.error", errorPage);
// Init pronto
$.pronto({
@@ -14,15 +15,19 @@
});
function requestPage() {
- console.log("load");
+ console.log("Request new page");
}
function initPage() {
// bind events and initialize plugins
- console.log("init");
+ console.log("Render new page");
}
function destroyPage() {
// unbind events and remove plugins
- console.log("destroy");
+ console.log("Destroy old page");
+ }
+
+ function errorPage() {
+ console.error("Error loading page");
}
View
@@ -1,5 +1,5 @@
/*
- * Pronto v3.0.9 - 2014-02-06
+ * Pronto v3.0.10 - 2014-03-12
* A jQuery plugin for faster page loads. Part of the formstone library.
* http://formstone.it/pronto/
*
@@ -49,6 +49,7 @@
* @event pronto.request "Before request is made; triggered on window"
* @event pronto.load "After request is loaded; triggered on window"
* @event pronto.render "After state is rendered; triggered on window"
+ * @event pronto.error "After load error; triggered on window"
*/
var pub = {
@@ -158,7 +159,7 @@
if (data && data.url !== currentURL) {
if (options.force) {
// Force a new request, even if navigating back
- _request(data.url, data.scroll, false);
+ _request(data.url);
} else {
// Fire request event
$window.trigger("pronto.request");
@@ -173,10 +174,8 @@
* @name _request
* @description Requests new content via AJAX
* @param url [string] "URL to load"
- * @param scrollTop [int] "Current scroll position"
- * @param doPush [boolean] "Flag to replace or add state"
*/
- function _request(url, scrollTop, doPush) {
+ function _request(url) {
// Fire request event
$window.trigger("pronto.request");
@@ -186,13 +185,19 @@
dataType: "json",
success: function(response) {
response = (typeof response === "string") ? $.parseJSON(response) : response;
- scrollTop = (typeof scrollTop !== "undefined") ? scrollTop : 0;
- doPush = (typeof doPush !== "undefined") ? doPush : true;
- _process(url, response, scrollTop, doPush);
+ _process(url, response, 0, true);
},
- error: function(response) {
- window.location.href = url;
+ error: function(jqXHR, status, error) {
+ $window.trigger("pronto.error", [ error ]);
+
+ // Try to parse response text
+ try {
+ var response = $.parseJSON(jqXHR.responseText);
+ _process(url, response, 0, true);
+ } catch (e) {
+ //console.error(e);
+ }
}
});
}
@@ -1,9 +1,9 @@
/*
- * Pronto v3.0.9 - 2014-02-06
+ * Pronto v3.0.10 - 2014-03-12
* A jQuery plugin for faster page loads. Part of the formstone library.
* http://formstone.it/pronto/
*
* Copyright 2014 Ben Plum; MIT Licensed
*/
-!function(a,b){"use strict";function c(c){l&&(a.extend(!0,n,c||{}),n.$body=a("body"),n.$container=a(n.container),n.render===a.noop&&(n.render=h),m=b.location.href,i(),k.on("popstate.pronto",e),n.$body.on("click.pronto",n.selector,d))}function d(a){var c=a.currentTarget;if(!(a.which>1||a.metaKey||a.ctrlKey||a.shiftKey||a.altKey||b.location.protocol!==c.protocol||b.location.host!==c.host||"_blank"===c.target)){if(c.hash&&c.href.replace(c.hash,"")===b.location.href.replace(location.hash,"")||c.href===b.location.href+"#")return i(),void 0;a.preventDefault(),a.stopPropagation(),a.stopImmediatePropagation(),m===c.href?i():f(c.href)}}function e(a){var b=a.originalEvent.state;b&&b.url!==m&&(n.force?f(b.url,b.scroll,!1):(k.trigger("pronto.request"),g(b.url,b.data,b.scroll,!1)))}function f(c,d,e){k.trigger("pronto.request"),a.ajax({url:c+(c.indexOf("?")>-1?"&"+n.requestKey+"=true":"?"+n.requestKey+"=true"),dataType:"json",success:function(b){b="string"==typeof b?a.parseJSON(b):b,d="undefined"!=typeof d?d:0,e="undefined"!=typeof e?e:!0,g(c,b,d,e)},error:function(){b.location.href=c}})}function g(a,b,c,d){k.trigger("pronto.load"),j(a),i(),n.render.call(this,b),m=a,d?history.pushState({url:m,data:b,scroll:0},"state-"+m,m):i(),k.trigger("pronto.render").scrollTop(c)}function h(b){if("undefined"!=typeof b)for(var c in n.target)n.target.hasOwnProperty(c)&&b.hasOwnProperty(c)&&a(n.target[c]).html(b[c])}function i(){var b=[];for(var c in n.target)n.target.hasOwnProperty(c)&&(b[c]=a(n.target[c]).html());history.replaceState({url:m,data:b,scroll:k.scrollTop()},"state-"+m,m)}function j(a){if(a=a.replace(b.location.protocol+"//"+b.location.host,""),n.tracking.legacy)b._gaq=b._gaq||[],b._gaq.push(["_trackPageview",a]);else if(n.tracking.manager){var c={};c[n.tracking.variable]=a,b.dataLayer=b.dataLayer||[],b.dataLayer.push(c),b.dataLayer.push({event:n.tracking.event})}else"function"==typeof ga&&ga("send","pageview",a)}var k=a(b),l=b.history&&b.history.pushState&&b.history.replaceState,m="",n={force:!1,selector:"a",render:a.noop,requestKey:"pronto",target:{title:"title",content:"#pronto"},tracking:{legacy:!1,manager:!1,variable:"currentURL",event:"PageView"}},o={defaults:function(b){return n=a.extend(n,b||{}),a(this)},load:function(a){l?a&&f(a):b.location.href=a}};a.pronto=function(a){return o[a]?o[a].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof a&&a?this:c.apply(this,arguments)}}(jQuery,this);
+!function(a,b){"use strict";function c(c){l&&(a.extend(!0,n,c||{}),n.$body=a("body"),n.$container=a(n.container),n.render===a.noop&&(n.render=h),m=b.location.href,i(),k.on("popstate.pronto",e),n.$body.on("click.pronto",n.selector,d))}function d(a){var c=a.currentTarget;if(!(a.which>1||a.metaKey||a.ctrlKey||a.shiftKey||a.altKey||b.location.protocol!==c.protocol||b.location.host!==c.host||"_blank"===c.target)){if(c.hash&&c.href.replace(c.hash,"")===b.location.href.replace(location.hash,"")||c.href===b.location.href+"#")return i(),void 0;a.preventDefault(),a.stopPropagation(),a.stopImmediatePropagation(),m===c.href?i():f(c.href)}}function e(a){var b=a.originalEvent.state;b&&b.url!==m&&(n.force?f(b.url):(k.trigger("pronto.request"),g(b.url,b.data,b.scroll,!1)))}function f(b){k.trigger("pronto.request"),a.ajax({url:b+(b.indexOf("?")>-1?"&"+n.requestKey+"=true":"?"+n.requestKey+"=true"),dataType:"json",success:function(c){c="string"==typeof c?a.parseJSON(c):c,g(b,c,0,!0)},error:function(c,d,e){k.trigger("pronto.error",[e]);try{var f=a.parseJSON(c.responseText);g(b,f,0,!0)}catch(h){}}})}function g(a,b,c,d){k.trigger("pronto.load"),j(a),i(),n.render.call(this,b),m=a,d?history.pushState({url:m,data:b,scroll:0},"state-"+m,m):i(),k.trigger("pronto.render").scrollTop(c)}function h(b){if("undefined"!=typeof b)for(var c in n.target)n.target.hasOwnProperty(c)&&b.hasOwnProperty(c)&&a(n.target[c]).html(b[c])}function i(){var b=[];for(var c in n.target)n.target.hasOwnProperty(c)&&(b[c]=a(n.target[c]).html());history.replaceState({url:m,data:b,scroll:k.scrollTop()},"state-"+m,m)}function j(a){if(a=a.replace(b.location.protocol+"//"+b.location.host,""),n.tracking.legacy)b._gaq=b._gaq||[],b._gaq.push(["_trackPageview",a]);else if(n.tracking.manager){var c={};c[n.tracking.variable]=a,b.dataLayer=b.dataLayer||[],b.dataLayer.push(c),b.dataLayer.push({event:n.tracking.event})}else"function"==typeof ga&&ga("send","pageview",a)}var k=a(b),l=b.history&&b.history.pushState&&b.history.replaceState,m="",n={force:!1,selector:"a",render:a.noop,requestKey:"pronto",target:{title:"title",content:"#pronto"},tracking:{legacy:!1,manager:!1,variable:"currentURL",event:"PageView"}},o={defaults:function(b){return n=a.extend(n,b||{}),a(this)},load:function(a){l?a&&f(a):b.location.href=a}};a.pronto=function(a){return o[a]?o[a].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof a&&a?this:c.apply(this,arguments)}}(jQuery,this);
View
@@ -2,7 +2,7 @@
"name": "Pronto",
"id": "pronto",
"codename": "jquery.fs.pronto",
- "version": "3.0.9",
+ "version": "3.0.10",
"description": "A jQuery plugin for faster page loads. Part of the formstone library.",
"keywords": [
"performance",
View
@@ -1,6 +1,6 @@
{
"name": "pronto",
- "version": "3.0.9",
+ "version": "3.0.10",
"title": "Pronto",
"author": {
"name": "Ben Plum",
@@ -41,6 +41,7 @@
* @event pronto.request "Before request is made; triggered on window"
* @event pronto.load "After request is loaded; triggered on window"
* @event pronto.render "After state is rendered; triggered on window"
+ * @event pronto.error "After load error; triggered on window"
*/
var pub = {
@@ -150,7 +151,7 @@
if (data && data.url !== currentURL) {
if (options.force) {
// Force a new request, even if navigating back
- _request(data.url, data.scroll, false);
+ _request(data.url);
} else {
// Fire request event
$window.trigger("pronto.request");
@@ -165,10 +166,8 @@
* @name _request
* @description Requests new content via AJAX
* @param url [string] "URL to load"
- * @param scrollTop [int] "Current scroll position"
- * @param doPush [boolean] "Flag to replace or add state"
*/
- function _request(url, scrollTop, doPush) {
+ function _request(url) {
// Fire request event
$window.trigger("pronto.request");
@@ -178,13 +177,19 @@
dataType: "json",
success: function(response) {
response = (typeof response === "string") ? $.parseJSON(response) : response;
- scrollTop = (typeof scrollTop !== "undefined") ? scrollTop : 0;
- doPush = (typeof doPush !== "undefined") ? doPush : true;
- _process(url, response, scrollTop, doPush);
+ _process(url, response, 0, true);
},
- error: function(response) {
- window.location.href = url;
+ error: function(jqXHR, status, error) {
+ $window.trigger("pronto.error", [ error ]);
+
+ // Try to parse response text
+ try {
+ var response = $.parseJSON(jqXHR.responseText);
+ _process(url, response, 0, true);
+ } catch (e) {
+ //console.error(e);
+ }
}
});
}

0 comments on commit 04c43b5

Please sign in to comment.