Backbone.js View Deleting and Unpinning

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

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

Separately (via Datepicker) I want to want to populate the same collection with different models and create an instance of the view again.

The problem is how to close the source pagViewand delete pagCollbefore opening a new one, because this "ghost" creates problems for me. Are the variables mentioned above local variables? Is that what I need to create global pagColland reset()this?

+5
source share
4 answers

, , , :

  • ( jQuery, jquery DOM)

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

    DOM DOM.

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

    , , (), , this.trigger('myCustomEvent', params);

, , Derrick Bailey : http://lostechies.com/derickbailey/2011/09/15/zombies-run-managing-page-transitions-in-backbone-apps/.

, reset event

+13

. view.undelegateEvents().

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

+1

stopListening , DOM.

view.stopListening();

. stopListening , ... , , , , , .

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

0

, , Pub/Sub.

, View, .

For example, PubSub.subscribe("EVENT NAME", EVENT ACTIONS, CONDITION);in the condition function, you can check if everything is 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 call pub / sub through PubSub.pub("addSomething");in other places and not worry about duplicate actions.

There are trade-offs, of course, but it doesn't seem to be that difficult.

0
source

All Articles