Last active
November 8, 2015 19:13
-
-
Save chris-kobrzak/6b544d4b310b0543e9b0 to your computer and use it in GitHub Desktop.
Sample implementation of the browser window resize observer that throttles native browser events and emits custom events instead. This might be useful if you don't need to respond to resize events immediately and performance is a major concern.
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
WindowResizeManager = function( debounceWait ) { | |
this.debounceWait = debounceWait || 200; | |
_.bindAll(this, "handleWindowResizeEvent", "triggerWindowResizeCompleteEvent"); | |
this.cacheDom(); | |
this.bindWindowResizeEvent(); | |
}; | |
WindowResizeManager.prototype.cacheDom = function() { | |
this.window: $(window); | |
}; | |
WindowResizeManager.prototype.bindWindowResizeEvent = function() { | |
var handler = this.handleWindowResizeEvent(); | |
this.window.on( "resize orientationchange", handler ); | |
}; | |
WindowResizeManager.prototype.handleWindowResizeEvent = function() { | |
return _.debounce( this.triggerWindowResizeCompleteEvent, this.debounceWait ); | |
}; | |
WindowResizeManager.prototype.triggerWindowResizeCompleteEvent = function() { | |
this.window.trigger( "resizeCompleted.window" ); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The code implicitly depends on Underscore (
debounce
method) and jQuery and this could certainly be improved.