Backbone.js View removing and unbinding

后端 未结 4 439
轮回少年
轮回少年 2021-02-03 15:36

when my page opens, I call the collection and populate the view:

var pagColl = new pgCollection(e.models); 
var pagView = new pgView({collection: pagColl});


        
相关标签:
4条回答
  • 2021-02-03 15:46

    Here's one alternative I would suggest to use, by using Pub/Sub pattern.

    You can set up the events bound to the View, and choose a condition for such events.

    For example, PubSub.subscribe("EVENT NAME", EVENT ACTIONS, CONDITION); in the condition function, you can check if the view is still in the DOM.

    i.e.

    var unsubscribe = function() {
        return (this.$el.closest("body").length === 0);
    };
    PubSub.subscribe("addSomething",_.bind(this.addSomething, this), unsubscribe);
    

    Then, you can invoke pub/sub via PubSub.pub("addSomething"); in other places and not to worry about duplicating actions.

    Of course, there are trade-offs, but this way not seems to be that difficult.

    0 讨论(0)
  • 2021-02-03 15:48

    well there has been many discussion on this topic actually, backbone does nothing for you, you will have to do it yourself and this is what you have to take care of:

    1. removing the view (this delegates to jQuery, and jquery removes it from the DOM)

      // to be called from inside your view... otherwise its  `view.remove();`
      this.remove();
      

      this removes the view from the DOM and removes all DOM events bound to it.

    2. removing all backbone events

      // to be called from inside the view... otherwise it's  `view.unbind();`
      this.unbind();
      

      this removes all events bound to the view, if you have a certain event in your view (a button) which delegates to a function that calls this.trigger('myCustomEvent', params);

    if you want some idea's on how to implement a system I suggest you read up on Derrick Bailey's blogpost on zombie views: http://lostechies.com/derickbailey/2011/09/15/zombies-run-managing-page-transitions-in-backbone-apps/.

    another option

    another option would be to reuse your current view, and have it re-render or append certain items in the view, bound to the collection's reset event

    0 讨论(0)
  • 2021-02-03 15:59

    I was facing the same issue. I call the view.undelegateEvents() method.

    Removes all of the view's delegated events. Useful if you want to disable or remove a view from the DOM temporarily.

    0 讨论(0)
  • 2021-02-03 16:01

    I use the stopListening method to solve the problem, usually I don't want to remove the entire view from the DOM.

    view.stopListening();
    

    Tell an object to stop listening to events. Either call stopListening with no arguments to have the object remove all of its registered callbacks ... or be more precise by telling it to remove just the events it's listening to on a specific object, or a specific event, or just a specific callback.

    http://backbonejs.org/#Events-stopListening

    0 讨论(0)
提交回复
热议问题