Closed markreg closed 10 years ago
Not at the moment. You could use a wrapper around your model to do something similar for now. I made this a while ago: https://github.com/anthonyshort/emitter-manager
You could change the adapter to sub to that and then when you’re done call manager.off()
to clean up your model.
var manager = new Manager();
reactive.sub(function(model, type, fn){
manager.on(model, type, fn);
});
Maybe this could be a good thing to get into Reactive.
I was thinking about using component/removed
to unbind all subscriptions from the model when the element is removed from the DOM. That way cleanup is automatic.
var Reactive = require('reactive');
var removed = require('removed');
Reactive.prototype.sub = function(prop, fn){
var subs = this.el.subs = this.el.subs || {};
subs[prop] = fn;
this.adapter.subscribe(this.model, prop, fn);
return this;
}
function View(model){
this.el = el;
this.model = model;
this.reactive = Reactive(el, model, this);
removed(el, this.unsub);
}
View.prototype.unsub = function(){
var subs = this.el.subs;
for(var prop in subs){
this.reactive.unsub(prop, subs[prop]);
}
}
That could work too. Ideally there would be a cleaner way. You generally shouldn't need to worry about this though unless you're re-using the model across views.
I usually use a 'view-model' with Reactive views and then sync up with the real model when needed to avoid needing to do this. The added benefit that is usually you store a lot of view state in your Reactive models (selected tabs etc) that you don't want on your Model.
I'm using reactive to render a list of of models, which means for each model there's one reactive instance, so we can made changes to any of the model and see the changes in the list view immediately. I need to provide the remove method for each model and sometimes refresh all the list with a new collection of models, so I really hope that reactive could provide an easier way to unsubscribe all the listeners.
This will exist in reactive 1.0 via a destroy
method to unbind all handlers and remove the element from the dom.
Is there a way to unbind all listener functions from the model for a particular reactive element/view?