Skip to content

Instantly share code, notes, and snippets.

@jesgundy
Created April 1, 2014 19:19
Show Gist options
  • Save jesgundy/9921079 to your computer and use it in GitHub Desktop.
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.
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