Created
November 12, 2013 15:15
-
-
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).
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
// 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