我有一个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)
解决方法
使用jQuery注册自己的事件处理程序没有什么问题.
Ember当前没有任何窗口事件处理.
我也建议尝试提出一个不依赖全局变量的解决方案.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。