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()

How do I get my repositionBoards method called on resize events?

mu is too short
  • 426,620
  • 70
  • 833
  • 800
wedep.java
  • 51
  • 1
  • 2

1 Answers1

8

The resize event is sent to window:

The resize event is sent to the window element when the size of the browser window changes

But a Backbone view's events are bound to the view's el using delegate. The view's el won't get a resize event so putting 'resize articule': 'repositionBoards' in your view's events won't do any good.

If you need to get the resize event in your view, you'll have to bind it to window yourself:

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()

Also note the addition of a remove so that you can unbind your resize handler. You will, of course, want to use view.remove() to remove your view or just don't worry about it if that view is your whole application.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • Thank you for the thorough answer. My only concern is that my articles' sizes change when text is entered into them. When the size expands, I want to call reposition boards. But since resize is sent to the window, this does me no good. Do you know if there are any events that can be tied to the resizing of an article or div? Just when it expands like when the height or width increases. Thanks again. – wedep.java Jun 28 '12 at 23:52
  • @user1489926: But there is no resize event on anything other than the window, AFAIK you have to do it the hard way: http://stackoverflow.com/q/172821/479863 – mu is too short Jun 29 '12 at 00:13