如何从我的骨干视图解除所有socket.io事件的绑定?

时间:2013-03-04 05:54:25

标签: backbone.js socket.io backbone-views backbone-events

我有一个页面,其中包含两个主干视图(与两个模板相关的视图)。我正在根据另一个视图上不同项目上的点击事件来更改一个视图的内容。为此,每次单击一个视图中的任何项目时,我只创建另一个视图的实例,其中包含一些socket.io事件。它第一次运行良好,但每次我点击第一个视图上的项目时,它只是创建第二个实例,以便所有socket.io事件都绑定。除非每次点击第一个视图中的项目并调用socket.io事件时首次点击,否则根据我对不同项目的点击次数,它会多次点击。

我知道每次单击一个项目时,它都会创建一个带有socket.io事件绑定的视图实例。但我无法解开以前的socket.io事件。

我试过使用这个引用: Backbone.js View removing and unbinding 但它不适用于我的情况。可能是我没有以正确的方式使用它。

任何人都可以给我一个解决方案或解除绑定之前绑定的所有socket.io事件的方法吗?

这是我的Clicking事件,我在这里创建了另一个视图的新实例,其中所有的socket.io事件都绑定了。

 LoadQueueDetails: function (e) {
    e.preventDefault();
    var queues = new Queues();

    queues.fetch({
        data: $.param({ Code: this.model.get("QueueCode") }),
        success: function () {
            $("#grid21").html(new SearchResultListView({ collection: queues }).el);
        },
        error: function (queues) {
            alert('error found in fetch queue details');
        }
    });
   }

这是我实际的视图,我绑定了所有socket.io事件。

window.SearchResultListView = Backbone.View.extend({

initialize: function () {
    this.collection.on('change', this.render, this);
    this.render();
},

render: function () {
    var Queues = this.collection;
    var len = Queues.length;

    $(this.el).html(this.template());

    for (var i = 0; i < len; i++) {
        $('.QueueListItem', this.el).append(new SearchResultListItemView({ model: Queues.models[i]}).render().el);
    }
    return this;
 }
});


window.SearchResultListItemView = MainView.extend({
tagName: "tr",

initialize: function () {

    this.__initialize();

    var user;
    if ($.super_cookie().check("user_cookie")) {
        this.user = $.super_cookie().read_JSON("user_cookie");
    }     

    this.model.bind("change", this.render, this);
    this.model.on("destroy", this.close, this);
    socket.emit('adduser', this.user.UserName, this.model.get("Code"));
},

events: {
    "click a": "JoinQueue"
},

onClose: function(){
    this.model.unbind("change", this.render);
},
close: function () {
    this.remove();
    this.unbind();
    this.model.unbind("change", this.render);
},
socket_events: {
    "updatechat": "updatechat",
    "changeroom": "changedroom"
},
changedroom: function (username, data) {
    alert(data);
    socket.emit('switchRoom', data);
},

updatechat: function (username, data) {
    alert(username);
    alert(data);
},

JoinQueue: function (e) {
    e.preventDefault();

    if ($.super_cookie().check("user_cookie")) {
        user = $.super_cookie().read_JSON("user_cookie");
    }

    socket.emit('sendchat', "new user");
},

render: function () {
    var data = this.model.toJSON();
    _.extend(data, this.attributes);
    $(this.el).html(this.template(data));
    return this;
}
});


window.Queue = Backbone.Model.extend({

urlRoot: "/queue",
initialize: function () {
},

defaults: {
    _id:null,
    Code: null,
    ServiceEntityId: null,
    ServiceEntityName:null,
    Name: null,
    NoOfWaiting: null,
    ExpectedTimeOfService: null,
    Status: null,
    SmsCode: null
}

});

window.Queues = Backbone.Collection.extend({
model: Queue,
url: "/queue",

initialize: function () {
}
});

Backbone.View.prototype.close = function () {
this.remove();
this.unbind();
if (this.onClose) {
    this.onClose();
}
}

这是我在searchResultItemview中绑定socket.io事件的主要观点。

var MainView = Backbone.View.extend({
initialize: function () {
    this.__initialize();
},

__initialize: function () {
    if (this.socket_events && _.size(this.socket_events) > 0) {
        this.delegateSocketEvents(this.socket_events);
    }
},

delegateSocketEvents: function (events) {

    for (var key in events) {
        var method = events[key];
        if (!_.isFunction(method)) {
            method = this[events[key]];
        }

        if (!method) {
            throw new Error('Method "' + events[key] + '" does not exist');
        }

        method = _.bind(method, this);
        socket.on(key, method);
    };
}
});

了解更多信息:

1. I am opening socket connection globally. Like this :
   var socket = io.connect('http://localhost:3000');

我正在等待任何建议或解决方案来摆脱这个问题。请随时询问任何问题。

1 个答案:

答案 0 :(得分:2)

关闭视图时,基本上每socket.removeListenersocket.on都需要{。}}。

您可以更新MainView并添加close方法。

这就是它在我的代码(CoffeeScript)中的外观

close: ->
    self = @
    _.each @socket_events, (method, key) ->
        method = self[self.socket_events[key]]
        socket.removeListener key, method