简体   繁体   English

有没有办法收听 Html 元素样式更改 - Backbone.JS

[英]Is there a way to listen to Html Element Style changes - Backbone.JS

I have the following HTML template:我有以下 HTML 模板:

    <li id="imovel" style="<%=  display %>">
        <div class="thumbnail">                      
          <a href="#">
            <img src="http://placehold.it/90x60"></img>
            <%= textvar %>
          </a>
        </div>                                    
    </li>

The display is a display: none or display: block depending on some things.显示是display: nonedisplay: block取决于某些事情。

I would like this view:我想要这样的观点:

var imovelView = Backbone.View.extend({
    
    el: $('#imovel')
    
    ,template:  _.template( $("#listing_template").html())
    
    ,events: {
        "change:display #imovel" : "activate"
    }
    
    ,initialize: function(){
        this.on('change:display', this.toggle, this);
        this.collection.bind('add', this.render, this);
    }
    
    ,activate: function(item){
        alert("change display");
    }

    ,render: function(item){
        var showPerPage = $('#show_per_page').val();
        var totalEntries = this.collection.length;

        var pageMax = Math.ceil( showPerPage/totalEntries );
        var displayValue = "display: none;";

        if(totalEntries <= showPerPage){
            displayValue = "display: block;";
        }

        var variables = { textvar: item.get("Lat"), display: displayValue };        
        var template = this.template( variables );

        $('#max_page').val(pageMax);
        $('#content').append( template );
        resizeViewport();
    }
    
});

When I do change:display in display I mean Listen to changes in the el style display property.当我change:display in display 我的意思是听 el 样式显示属性的变化。

Is it possible to do that ?有可能这样做吗?

Can you alter the code to trigger event on the element that is being hidden/shown?您可以更改代码以在被隐藏/显示的元素上触发事件吗? This is as much as adding two lines when display nonde/hide is set.这与设置 display nonde/hide 时添加两行一样多。 $(el).trigger('display:changed') on the element that is changing it's display state. $(el).trigger('display:changed')在改变其显示状态的元素上。 And then from Backbone.View just listen for this custom event on the element disapearingElement.on('display:changed', doSomething)然后从 Backbone.View 只在元素disapearingElement.on('display:changed', doSomething)上监听这个自定义事件

Any other solution will be supper messy and put a constant load on the browser.任何其他解决方案都会非常混乱,并且会给浏览器带来持续的负载。 You'd have to create helper method which would use polling with setInterval/setTimeout that would check the state of element all the time and trigger a callback/custom event when it changes - this is the only way to implement it cross browser.您必须创建辅助方法,该方法将使用带有 setInterval/setTimeout 的轮询,该方法将始终检查元素的状态并在元素更改时触发回调/自定义事件 - 这是跨浏览器实现它的唯一方法。

But honestly... don't leave messy code - if it's fine for you, ok - but think about people who will be working with this code when you are gone :P但老实说......不要留下凌乱的代码 - 如果这对你没问题,好吧 - 但想想当你离开时会使用这些代码的人:P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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