Skip to content

Instantly share code, notes, and snippets.

@jugglinmike
Last active August 29, 2015 13:57
Show Gist options
  • Save jugglinmike/9810837 to your computer and use it in GitHub Desktop.
Save jugglinmike/9810837 to your computer and use it in GitHub Desktop.
A Backbone Event wrapper for browser window events
define(['jquery', 'underscore', 'backbone'], function($, _, Backbone) {
'use strict';
var WindowEmitter = _.extend({}, Backbone.Events);
var $window = $(window);
// 0.1 second is about the limit for having the user feel that the
// system is reacting instantaneously, meaning that no special feedback
// is necessary except to display the result.
// Source: http://www.nngroup.com/articles/response-times-3-important-limits/
var period = 100;
var triggerResize = _.bind(WindowEmitter.trigger, WindowEmitter, 'resize');
var triggerScroll = _.bind(WindowEmitter.trigger, WindowEmitter, 'scroll');
$window.on('resize', _.throttle(triggerResize, period));
$window.on('scroll', _.throttle(triggerScroll, period));
return WindowEmitter;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment