Some of my views need their text fields to be converted to text editors.
I am using jwysiwyg as an editor. This requires that the element to which it is attached is on the page when the editor is initialized, that is, when I call $ (this.el) .wysiwyg (), this.el is already in the document.
Most of my views are not actually attached to dom - their visualization methods simply set their html content elements using application template templates, for example. $ (this.el) .html (this.template (content)
Views / Controllers continue to follow the chain, actually inserting these child views on the page. At the same time, looks re-display themselves when their models change.
How to ensure that the editor is attached to the element each time it is rendered and still makes sure that the editor is not attached until the element is on the page?
Obviously, I could hack something together that would work in this particular case, but I would like an elegant solution to work for all cases.
Any help would be greatly appreciated.
Edit: The main thing is that the solution must be gracefully scaled to cover several elements that should be styled after rendering and should not be written until they are in the DOM
Edit: this is not a problem if I am doing top-down rendering, but it is slow, I would like it to be able to render from bottom to top and then paste the full view in one pass at the top
Edit:
Using a combination of some of the methods below, I'm going to do something like the following. Any comments / criticism would be appreciated.
app/views/base_view.js: initialize: function() { // wrap the render function to trigger 'render' events this.render = _.wrap(this.render, function() { this.trigger('render') }); // bind this view to 'attach' events. // 'attach' events must be triggered manually on any view being inserted into the dom this.bind('attach', function() { this.attached(); // the refreshed event is only attached to the render event after the view has been attached this.bind('render', this.refreshed()) // each view must keep a record of its child views and propagate the 'attach' events _.each(this.childViews, function(view) { view.trigger('attach') }) }) } // called when the view is first inserted to the dom attached: function() { this.style(); } // called if the view renders after it has been inserted refreshed: function() { this.style(); } style: function() { // default styling here, override or extend for custom }