gmac / backbone.epoxy

Declarative data binding and computed models for Backbone
http://epoxyjs.org
MIT License
615 stars 89 forks source link

manualy trigger to redraw view #87

Open alxkolm opened 10 years ago

alxkolm commented 10 years ago

I change my model silently. How can I manualy tell Epoxy.js to redraw my view?

Sorry for my english.

maxpoulin64 commented 10 years ago

The only way I could think of would be to remove the bindings then add them again. But the performance will be terrible, because it does a lot of things and adds event handlers to both your models and the DOM.

view.removeBindings();
view.applyBindings();

Internally Epoxy uses backbone's listenTo() method with internal functions you can't access from the outside, so the only way to manually trigger them would be to trigger them on the model. Or play with Backbone's internals and manually call the callback epoxy put there, but that's hacky.

Having the models updated silently seems like a really bad idea to start with. If you really can't trigger events on the original collection, I'd suggest making a clone of it and put that as the viewModel of your views for Epoxy to use. Then to update the views, you can just copy your model into the viewModel and that will trigger the events so Epoxy can update the DOM correctly. If you need two way bindings you will need to forward the events from the viewModel to your real model manually however.

var view = new Epoxy.View({viewModel: new Backbone.Model(yourModel.attributes)});
// Later...
view.viewModel.set(yourModel.attributes);

This will update all the fields on the viewModel with fresh values without ever touching your original model.

I hope this helps solve your problem.