Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Changed zepto ajax to javascript xmlhttprequest and moved success and…
… error callbacks to properties in the MeteorRider object.
  • Loading branch information
DanielOchoa committed Jun 25, 2014
commit 66291e888bc3899bf4062f2f2b5506fbbbb13e2c
101 changes: 58 additions & 43 deletions www/js/meteor-rider.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,56 +30,71 @@

//define('MeteorRider', ['jquery'], function($) {
var MeteorRider = {

init: function(currentPath) {
var meteorUrl = __MeteorRiderConfig__.meteorUrl;
if (! (meteorUrl.length > 0)) {

this.meteorUrl = __MeteorRiderConfig__.meteorUrl;

if (! (this.meteorUrl.length > 0)) {
console.error('MeteorRider: error: unable to determine config.meteorUrl');
return false;
}
// trigger request
$.ajax({
url: meteorUrl,
cache: false,
// TODO: split to method on MeteorRider
error: function( jqXHR, textStatus, errorThrown ) {
console.error("MeteorRider failure");
console.error(jqXHR, textStatus, errorThrown);
},
// TODO: split to method on MeteorRider
success: function( data, textStatus, jqXHR ) {
console.log("MeteorRider success");
console.log(textStatus);
console.log(data);
// update URLs to include domain prefix
data = data.replace(/(href|src|manifest)\=\"\//gm, '$1="' + meteorUrl + '/');
console.log(meteorUrl);
console.log(data);

// set 'currentPath' to empty string if not passed
currentPath = (typeof currentPath === 'string' ? currentPath : '');
// set the window.location object correctly so iron-router
// and other packages that depend on window.location work correctly
if (typeof window.history.replaceState === 'function') {
// window.history.replaceState() not supported in all clients
window.history.replaceState({}, "", meteorUrl + currentPath);
} else {
// TODO: should we do window.history.add() or something?
}
// non zepto/jquery request
request = new XMLHttpRequest();
request.open('GET', this.meteorUrl, true);
request.onload = this.onSuccess;
request.onerror = this.onError;
request.send();

},

onSuccess: function () {
if (request.status >= 200 && request.status < 400) {
var data = request.responseText;
///////////////////
console.log("MeteorRider success");
console.log(data);
// update URLs to include domain prefix
data = data.replace(/(href|src|manifest)\=\"\//gm, '$1="' + this.meteorUrl + '/');
console.log(this.meteorUrl);
console.log(data);

// replace the document with the new document/data
// this is the REAL hijacking...
// all old JS remains (unless overwritten, name collision)
// all HTML is replaced/overwritten
// all new CSS/JS is loaded
document.open();
document.write(data);
document.close();
// trigger the "loaded" events (it'd be nice to do this AFTER JS has loaded
$(document).trigger('DOMContentLoaded');
$(document).trigger('load');
$(document).trigger('complete');
// set 'currentPath' to empty string if not passed
currentPath = (typeof currentPath === 'string' ? currentPath : '');
// set the window.location object correctly so iron-router
// and other packages that depend on window.location work correctly
if (typeof window.history.replaceState === 'function') {
// window.history.replaceState() not supported in all clients
window.history.replaceState({}, "", this.meteorUrl + currentPath);
} else {
// TODO: should we do window.history.add() or something?
}
});

// replace the document with the new document/data
// this is the REAL hijacking...
// all old JS remains (unless overwritten, name collision)
// all HTML is replaced/overwritten
// all new CSS/JS is loaded
document.open();
document.write(data);
document.close();
// trigger the "loaded" events (it'd be nice to do this AFTER JS has loaded
$(document).trigger('DOMContentLoaded');
$(document).trigger('load');
$(document).trigger('complete');

} else {
// We reached our target server, but it returned an error
console.error(request.statusText);
}

},

onError: function () {
// There was a connection error of some sort
console.error("MeteorRider failure");
console.error(request.statusText);
}
}
//});