0

我有以下 HTML 模板:

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

显示是display: nonedisplay: block取决于某些事情。

我想要这样的观点:

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

当我change:display在 display 时,我的意思是听 el 样式显示属性的变化。

有可能这样做吗?

4

1 回答 1

2

您可以更改代码以在隐藏/显示的元素上触发事件吗?这与设置显示 nonde/hide 时添加两行一样多。$(el).trigger('display:changed')在改变其显示状态的元素上。然后从 Backbone.View 只监听元素上的这个自定义事件disapearingElement.on('display:changed', doSomething)

任何其他解决方案都会非常混乱,并且会给浏览器带来持续的负载。您必须创建辅助方法,该方法将使用 setInterval/setTimeout 进行轮询,该方法将始终检查元素的状态并在其更改时触发回调/自定义事件 - 这是跨浏览器实现它的唯一方法。

但老实说......不要留下凌乱的代码 - 如果它对你来说很好,好的 - 但是想想当你离开时将使用这些代码的人:P

于 2012-02-05T12:55:18.267 回答