简体   繁体   中英

Backbone view to re-render if any other view gets rendered

I have a TooltipView that pops up whenever an element with class .tooltip is moused-over and takes the content of that elements data-tooltip attribute and displays it in the tooltip.
I bind the events for TooltipView in it's initialise function but the problem is if another view is dynamically created, or re-rendered then TooltipView doesn't know about it.

var tooltipView =  Backbone.View.extend({
    el: '#tooltip-container',

    initialize: function() {
        $('.tooltip').on('mouseover', function() {
             ....
        }
    }
}

I know I can manually trigger events from each of the views when they're rendered but is there away to do it just from within TooltipView?
I'd like TooltipView to listen for ANY view to render and then re-render itself.

Maybe attaching events to higher DOM level would work for you:

initialize: function() {
    $('body').on('mouseover', '.tooltip', function() {
         ....
    }
}

Don't forget to unbind it later though

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM