Created
April 1, 2014 19:19
-
-
Save jesgundy/9921079 to your computer and use it in GitHub Desktop.
A require module that leverages backbone jquery and underscore to debounce and keep a running log of the window width for all components to listen to.
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
define([ | |
'backbone', | |
'jquery', | |
'underscore' | |
], function(Backbone, $, _) { | |
// x-browser window width test | |
function winWidth() { | |
var w = 0; | |
// IE | |
if (!window.innerWidth) { | |
if ( document.documentElement.clientWidth !== 0 ) { | |
// strict mode | |
w = document.documentElement.clientWidth; | |
} | |
else { | |
// quirks mode | |
w = document.body.clientWidth; | |
} | |
} | |
else { | |
// w3c | |
w = window.innerWidth; | |
} | |
return w; | |
}; | |
// Window Model | |
var WindowModel = Backbone.Model.extend({ | |
initialize: function() { | |
var self = this; | |
var lazyLayout = _.debounce(function(){ | |
self.getWidth(); | |
}, 150); | |
$(window).resize(lazyLayout); | |
this.getWidth(); | |
}, | |
getWidth: function() { | |
this.set( "width", winWidth() ); | |
} | |
}); | |
// return one instantiation for all to share | |
var windowModel = new WindowModel(); | |
return windowModel; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment