Skip to content

Instantly share code, notes, and snippets.

@kelexel
Last active August 29, 2015 14:10
Show Gist options
  • Save kelexel/43c6ca9ce0d0e633573b to your computer and use it in GitHub Desktop.
Save kelexel/43c6ca9ce0d0e633573b to your computer and use it in GitHub Desktop.
Updating History.js for Mootools 1.5.1
/*
---
name: History
description: History Management via popstate or hashchange.
authors: Christoph Pojer (@cpojer)
license: MIT-style license.
requires: [Core/Events, Core/Element.Event, Class-Extras/Class.Binds]
provides: History
...
*/
(function(){
var events = Element.NativeEvents,
location = window.location,
history = window.history,
hasPushState = ('pushState' in history),
event = hasPushState ? 'popstate' : 'hashchange';
this.History = new new Class({
// Removed Class.Binds from Implements since it's now implied in More
// Implements: [Class.Binds, Events],
Implements: [Events],
bound: ['pop'],
initialize: hasPushState ? function(){
events[event] = 2;
// update binding to new More
// window.addEvent(event, this.bound('pop'));
window.addEvent(event, this.pop);
} : function(){
events[event] = 1;
// update binding to new More
// window.addEvent(event, this.bound('pop'));
window.addEvent(event, this.pop);
this.hash = location.hash;
if ('onhashchange' in window) return;
this.timer = this.periodical.periodical(200, this);
},
push: hasPushState ? function(url, title, state){
if (this.previous == url) return;
history.pushState(state || null, title || null, url);
this.previous = url;
} : function(url){
location.hash = url;
},
replace: hasPushState ? function(url, title, state){
history.replaceState(state || null, title || null, url);
} : function(url){
this.hash = '#' + url;
this.push(url);
},
pop: hasPushState ? function(event){
// this.onChange is undefined! this is "window"
// if (this.onChange)
// trying this.History.onChange instead
this.History.onChange(location.pathname, event.event.state);
} : function(){
var hash = location.hash;
if (this.hash == hash) return;
this.hash = hash;
// this.onChange is undefined! this is "window"
// if (this.onChange)
// trying this.History.onChange instead
this.History.onChange(hash.substr(1));
},
onChange: function(url, state){
this.fireEvent('change', [url, state]);
},
back: function(){
history.back();
},
forward: function(){
history.forward();
},
hasPushState: function(){
return hasPushState;
},
periodical: function(){
if (this.hash != location.hash) this.pop();
}
});
})();
@kelexel
Copy link
Author

kelexel commented Dec 7, 2014

Problem resides line 68, this.onChange is undefined.. why ? because of the way the Class is self-instantiated inside a Closure ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment