Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a resize event to an article in backbone?

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?

like image 902
wedep.java Avatar asked Jun 28 '12 23:06

wedep.java


1 Answers

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.

like image 94
mu is too short Avatar answered Sep 28 '22 08:09

mu is too short