我有一个mixin,它自动重新计算并设置页面上的div的高度大小.
它工作,但似乎愚蠢地绑定到一个jQuery事件,并在每次调用时手动触发Ember事件.
有没有办法直接在Ember中绑定窗口事件?
我有一个简化的JSFiddle here
这是代码:
App.windowWrapper = Ember.Object.create(Ember.Evented,resizeTimer: null init: -> @_super() object = this $(window).on 'resize',-> window.clearTimeout(object.resizeTimer) object.resizeTimer = setTimeout( App.windowWrapper.resize,100) true resize: -> App.windowWrapper.fire('resize') )
和那个叫做mixin的.
App.Scrollable = Ember.Mixin.create classNames: "scrollable" init: -> Ember.assert("Scrollable must be mixed in to a View",this instanceof Ember.View) @_super() didInsertElement: -> @_super() @calculateHeight() App.windowWrapper.on('resize',this,'calculateHeight') willDestroyElement: -> App.windowWrapper.off('resize','calculateHeight') @_super() calculateHeight: -> offset = @$().offset().top windowHeight = $(window).height() @$().css('height',windowHeight - offset)