简体   繁体   English

警报触发多次骨架

[英]Alert trigger multiple times backbonejs window

I got this weird thing yesterday. 昨天我得到了这个奇怪的东西。 I try several of time to fix this problem. 我尝试了几次来解决此问题。 When I came back the page same twice, my app trigger alert multiple times, depends how many times I visit the page. 当我两次回到同一页面时,我的应用会多次触发警报,具体取决于我访问该页面的次数。 I already done some research regarding to this 'zombie' thing and memory lack through this site and internet, but I found dead end. 我已经对这种“僵尸”事物进行了一些研究,并且通过该站点和互联网缺乏记忆,但是我发现死胡同了。 It's already 2 days can't fix this issue. 已经2天无法解决此问题。

My code 我的密码

View page 查看页面

initialize: function() {
    $(window).scroll(function() {
        if ($(window).scrollTop() + $(window).height() == $(document).height()) {
            alert("bottom!");
        }
    });
    this.bind("reset", this.updateView());
},
render: function() {
    this.$el.html(notificationListViewTemplate);
},
updateView: function() {
    console.log("clear");
    this.remove();
    this.render();
}

router 路由器

showNotificationList: function(actions) {
    var notificationListView = new NotificationListView();
    this.changePage(notificationListView);
},

Why it happen? 为什么会发生?

Calling View.remove will indeed undelegate events set by the view 调用View.remove确实可以取消删除视图设置的事件

remove view.remove() 删除 view.remove()
Removes a view from the DOM, and calls stopListening to remove any bound events that the view has listenTo'd. 从DOM中删除视图,并调用stopListening删除视图具有listenTo'd的所有绑定事件。

but it can only do so on the events it know about : the ones set by the events hash or by calling this.listenTo 但它只能对已知的事件执行此操作:由事件哈希或通过调用this.listenTo设置的事件

You set up a scroll listener but you never remove it, which means the past views will keep listening : see this demo of your predicament http://jsfiddle.net/nikoshr/E6MQ6/ 您设置了滚动侦听器,但从未删除过它,这意味着过去的视图将继续侦听:请参见此困境示例http://jsfiddle.net/nikoshr/E6MQ6/

In this case, you can't use the hash of events so you have to take care of the cleaning up yourself, for example by overriding the remove method : 在这种情况下,您不能使用事件的哈希,因此您必须自己清理,例如,通过重写remove方法:

var V = Backbone.View.extend({
    initialize: function() {
        $(window).scroll(function() {
        if ($(window).scrollTop() + $(window).height() == $(document).height()) {
            console.log("bottom!");
        }
        });
    },
    render: function() {
    },
    updateView: function() {
        console.log("clear");
        this.remove();
        this.render();
    },
    remove: function() {
        Backbone.View.prototype.remove.call(this);
        $(window).off('scroll'); // for example, will remove all listeners of the scroll event
    }
});

And a demo http://jsfiddle.net/nikoshr/E6MQ6/1/ 还有一个演示http://jsfiddle.net/nikoshr/E6MQ6/1/

And a slightly less brutal removing of the scroll event, by using a namespaced listener : 通过使用命名空间的侦听器,可以稍微减少残酷的滚动事件:

var V = Backbone.View.extend({
    initialize: function() {
        $(window).on('scroll.'+this.cid, function() {
            ...
        });
    },
    remove: function() {
        Backbone.View.prototype.remove.call(this);
        $(window).off('scroll.'+this.cid);
    }
});

http://jsfiddle.net/nikoshr/E6MQ6/2/ http://jsfiddle.net/nikoshr/E6MQ6/2/

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

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