Created
March 25, 2014 03:15
-
-
Save ryan-scott-dev/9754607 to your computer and use it in GitHub Desktop.
Maria Routing Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
function removeThrobber() { | |
var loading = document.getElementById('loading'); | |
loading.parentNode.removeChild(loading); | |
} | |
maria.on(window, 'load', function() { | |
application.app = bootstrapModel.createAppModel(); | |
application.app.load({ | |
success: function() { | |
removeThrobber(); | |
// You should only directly access the view for testing purposes! | |
application.appview = new application.AppView(application.app); | |
// Tell app to route to the current url | |
application.app.router().toWindowLocation(); | |
// Show the app to the user. | |
document.body.appendChild(application.appview.build()); | |
}, | |
failure: function(jqXHR, textStatus, errorThrown) { | |
removeThrobber(); | |
// Show an error message to the user. | |
document.body.appendChild(document.createTextNode('Error: Unable to load the app.')); | |
} | |
}); | |
}); | |
}); | |
}()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
application.Model.subclass(application, 'Router', { | |
attributes: { | |
_routes: [], | |
_defaultRoute: null | |
}, | |
properties: { | |
toWindowLocation: function() { | |
var locationPath = window.location.pathname; | |
var locationHash = window.location.hash; | |
var location = locationPath; | |
if (locationHash !== '') { | |
// Always direct to the location hash if is available | |
location = '/' + window.location.hash.slice(1); | |
} else if (!application.hasHistoryAPI()) { | |
// Convert the path to a hash if in IE | |
var url = window.location.pathname; | |
var hashPath = url.convertToHashPath(); | |
window.location.replace(hashPath); | |
location = locationPath; | |
} | |
this.routeAppTo(application.app, location); | |
}, | |
routeTo: function(path) { | |
var routes = this.getRoutes(); | |
var found = false; | |
for(var i = 0; i < routes.length; i++) { | |
var result = routes[i].pattern.exec(path); | |
if (result) { | |
found = true; | |
routes[i].callback(path, result.slice(1)); | |
break; | |
} | |
} | |
if (!found && this.getDefaultRoute()) { | |
this.getDefaultRoute()(path); | |
} | |
return found; | |
}, | |
addRoute: function(pattern, callback) { | |
this.getRoutes().push({ | |
pattern: pattern, | |
callback: callback | |
}); | |
}, | |
routeAppTo: function(app, path) { | |
this.addRoute(/^\/search\/(.*)/, function(path, match) { | |
app.navigate().toSearch(match[0]); | |
}); | |
this.addRoute(/^\/things\/([0-9]+)/, function(path, match) { | |
var id = match[0]; | |
var link = app.getRepository().getShowThingLink(); | |
var thing = application.ThingModel.fromId(id, link, { | |
success: function() { | |
if (!thing) return; | |
app.navigate().toThing(thing); | |
} | |
}); | |
}); | |
this.setDefaultRoute(function(path) { | |
app.navigate().toHome(); | |
}); | |
this.routeTo(path); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment