Skip to content

Instantly share code, notes, and snippets.

@SamHerbert
Created November 12, 2013 15:15
Show Gist options
  • Save SamHerbert/7432535 to your computer and use it in GitHub Desktop.
Save SamHerbert/7432535 to your computer and use it in GitHub Desktop.
Watch for window resize and fire a one time event (instead of a possibility of firing hundreds).
// Backbone, jQuery, and Underscore needed
var App = {'Vent': {}};
App.Vent = _.extend(App.Vent, Backbone.Events);
var bodyView = Backbone.View.extend({
el: $('body'),
initialize: function() {
this.resizePid = false;
$(window).on('resize', this.resize);
},
/**
* Watch for window resize and fire off event for
* other views to use new dimensions
*/
resize: function() {
if(this.resizePid > 0) {
clearTimeout(this.resizePid);
}
var _this = this;
this.resizePid = setTimeout(function() {
App.Vent.trigger('window:resize', {'width': $(window).width(), 'height': $(window).height()});
_this.resizePid = false;
}, 500);
}
});
var someView = Backbone.View.extend({
initialize: function() {
App.Vent.on('window:resize', this.windowResized);
},
windowResized: function(options) {
console.log(options);
// do resizing stuff here.
}
});
new bodyView();
new someView();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment