在Ember View中如何绑定窗口函数? [英] How should I bind to a window function in an Ember View?
问题描述
我有一个mixin,它会自动重新计算并设置页面大小的div的高度。
I have a mixin which automatically recalculates and sets the height of a div on page resize.
它的工作原理,但是对我来说,绑定到一个jQuery事件并在每次调用时手动触发Ember事件。
It works but it seems silly to me to be binding to a jQuery event and triggering an Ember event manually every time it is called.
有没有办法直接在Ember中绑定窗口事件?
Is there a way to bind to window events directly in Ember?
我有一个简化的JSFiddle here
I have a simplified 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。
And the mixin that is calling it.
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', this, 'calculateHeight')
@_super()
calculateHeight: ->
offset = @$().offset().top
windowHeight = $(window).height()
@$().css('height', windowHeight - offset)
推荐答案
使用jQuery注册自己的事件处理程序没有任何问题
There's nothing wrong with registering your own event handler with jQuery for something like this.
Ember目前没有任何窗口事件处理。
Ember doesn't have any window event handling currently.
我也建议试图找出一个不依赖全局变量的解决方案。
I'd also suggest trying to come up with a solution that didn't rely on globals.
这篇关于在Ember View中如何绑定窗口函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!