Skip to content

Instantly share code, notes, and snippets.

@belisarius222
Last active December 17, 2015 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save belisarius222/5553897 to your computer and use it in GitHub Desktop.
Save belisarius222/5553897 to your computer and use it in GitHub Desktop.
a super-bare-bones implementation of a reactive window.location (include this somewhere on the client in a Meteor app)
var Location = {};
this.Location = Location;
Location.dep = new Deps.Dependency;
Location.state = window.history.state;
window.onpopstate = function(event){
Location.state = event.state;
// note that popstate gets fired on initial page load in Webkit,
// but not in Firefox ...
Location.dep.changed();
};
Location.getState = function() {
var self = this;
self.dep.depend();
// Return the state we store, not the built-in, just in case
// we decide to store extra data with the state later.
return self.state;
};
Location.go = function( /* arguments */ ) {
var self = this;
window.history.go(arguments);
self.dep.changed();
};
Location.pushState = function( /* arguments */ ) {
var self = this;
window.history.pushState(arguments);
self.dep.changed();
};
Location.replaceState = function( /* arguments */ ) {
var self = this;
window.history.replaceState(arguments);
self.dep.changed();
};
Location.forward = function() {
var self = this;
window.history.forward();
self.dep.changed();
};
Location.back = function() {
var self = this;
window.history.back();
self.dep.changed();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment