Skip to content

Instantly share code, notes, and snippets.

@abstraktor
Created November 21, 2011 01:02
Show Gist options
  • Save abstraktor/1381316 to your computer and use it in GitHub Desktop.
Save abstraktor/1381316 to your computer and use it in GitHub Desktop.
ajax resender for backbone
/*
* This View handles failing backbone requests
* it will display the savestate on #savemsg
* it will resend failed packages after a exponential increasing amount of time
*/
window.View.SaveMsg = Backbone.View.extend({
el: $("#savemsg"),
events: {
"click a.resend": "resend",
},
failed_xhr: [],
resent: Date.now(),
reconnect_attempts: 0,
last_transfer: new Date(),
initialize: function() {
this.msg_welcome();
var self = this;
$(this.el).ajaxComplete(function() {
self.handle_complete.apply(self, arguments)
});
},
resend: function() {
this.resent = Date.now();
while (this.failed_xhr.length > 0)
$.ajax(this.failed_xhr.pop());
},
handle_complete: function(event, request, options) {
var result = parseInt(request.status / 100);
if (result == 4) {
this.failed_xhr.push(options);
this.begin_retry();
this.msg_lost_connection();
}
else if (result == 5) {
this.msg_internal_error(request.status);
}
else {
if (this.reconnect_attempts > 0) this.msg_reconnected();
else this.msg_saved();
this.reconnect_attempts = 0;
this.last_transfer = new Date();
}
},
begin_retry: function() {
//am i already retrying to send this stuff?
if (this.failed_xhr.length > 1)
return;
this.retry_in(5 * Math.pow(2, this.reconnect_attempts++));
},
retry_in: function(seconds, init_time) {
init_time || (init_time = Date.now())
//it was resent manually
if (this.resent > init_time) return;
$(this.el).html("Connection lost since " + this.time(this.last_transfer) + ". Retrying in " + seconds + " seconds (<a href='#' class='resend'>retry now</a>)");
if (seconds > 0) {
var self = this
window.setTimeout(function() {
self.retry_in.call(self, seconds - 1, init_time);
},
1000);
}
else {
this.resend();
};
},
time: function(date) {
d = date || new Date();
hours = d.getHours();
minutes = d.getMinutes() + "";
if (minutes.length == 1)
minutes = "0" + minutes;
return hours + ":" + minutes;
},
blinking: false,
blink: function(times, speed) {
if (!this.blinking) {
if (times > 0) {
self = this;
$(this.el).fadeToggle(speed, 'swing', function(){self.blinking = times!=1;})
this.blink(times - 1, speed)
this.blinking = true;
}
}
},
set_status_text: function(text) {
$(this.el).text(text)
this.blink(4, 500);
},
msg_welcome: function() {
this.set_status_text("Welcome at Osamos!");
},
msg_saved: function() {
this.set_status_text("saved at " + this.time());
},
msg_reconnected: function() {
this.set_status_text("saved at " + this.time());
$.flash("Reconnected! Your data are safe - at least for now.");
},
msg_lost_connection: function() {
$.flash("Connection lost since " + this.time(this.last_transfer) + ". Youre data may be lost if you don't reconnect us!", {
type: "error"
})
},
msg_internal_error: function(status) {
this.set_status_text("Internal Error ( " + status + " ). Try again or contact the support team!");
$.flash("An internal error occured! Your data are probably not saved...", {
type: "error"
})
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment