5
events:
    'click textarea': 'composeComment'
    'click .submit': 'submit'
    'blur textarea': 'textareaBlur'
    'resize article': 'repositionBoards'


repositionBoards: ->
    #this is my own function, i just need it to be called everytime an article's size changes
    board.align()

如何repositionBoards在调整大小事件时调用我的方法?

4

1 回答 1

8

事件resize发送到window

当浏览器窗口的大小发生变化时,该resize事件被发送到元素window

但是 Backbone 视图的事件绑定到视图的elusing delegate。视图el不会收到resize事件,因此放入'resize articule': 'repositionBoards'您的视图events不会有任何好处。

如果您需要resize在视图中获取事件,则必须将其绑定到window自己:

initialize: (options) ->
    $(window).on('resize', this.repositionBoards)
remove: ->
    $(window).off('resize', this.repositionBoards) # Clean up after yourself.
    @$el.remove() # The default implementation does this.
    @
repositionBoards: ->
    # Use => if you need to worry about what `@` is
    board.align()

还要注意添加 aremove以便您可以取消绑定resize处理程序。当然,如果该视图是您的整个应用程序,您当然会想要view.remove()删除您的视图,或者只是不要担心它。

于 2012-06-28T23:32:56.413 回答