Skip to content

Instantly share code, notes, and snippets.

@James1x0
Created July 17, 2014 16:43
Show Gist options
  • Save James1x0/b810e2d5b49c507164f5 to your computer and use it in GitHub Desktop.
Save James1x0/b810e2d5b49c507164f5 to your computer and use it in GitHub Desktop.
Window Event Binder Mixin
import Ember from 'ember';
// This mixin binds callbacks to events.
export default Ember.Mixin.create({
/*
Mixin setup
NOTE: Must be implemented on init, didTransition, or didInsertElement hooks
*/
setupWindowBindings: function () {
$(window).on('resize scroll', { emEl: this }, this._bindToProperties);
},
/*
Mixin teardown
NOTE: Can be implemented on willTransition or willDestroyElement hooks
*/
teardownWindowBindings: function () {
$(window).off('resize scroll', this._bindToProperties);
},
_bindToProperties: function ( event ) {
event.data.emEl.set('did' + event.type.charAt(0).toUpperCase() + event.type.slice(1), true);
}
});
@James1x0
Copy link
Author

  _bindToProperties: function ( event ) {
    var fn = eval('event.data.emEl.handleWindow' + event.type.charAt(0).toUpperCase() + event.type.slice(1));
    Ember.run.throttle(event.data.emEl, fn, 1000);
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment