私のビューのいくつかは、テキストエリアをリッチテキストエディタに変換する必要があります。
私はエディターとしてjwysiwygを使用しています。アタッチされている要素は、エディターが初期化されるときにページにある必要があります。つまり、$(this.el).wysiwyg()を呼び出すと、this.elは既にドキュメントにあります。
私のビューのほとんどは実際にはdomにアタッチされていません-それらのrenderメソッドは、アプリテンプレートエンジンを使用して要素のhtmlコンテンツを設定するだけです(例:$(this.el).html(this.template(content))
ビュー/コントローラーは、これらの子ビューを実際にページに挿入した後、チェーンルックのさらに上にあります。同時に、モデルが変更されると、ビューは再レンダリングされます。
レンダリングされるたびにエディターが要素にアタッチされていることを確認し、要素が既にページに表示されるまでエディターがアタッチされていないことを確認するにはどうすればよいですか?
明らかに、この特定のケースで機能する何かを一緒にハックすることはできますが、すべてのケースで機能するエレガントなソリューションが必要です。
どんな助けでも大歓迎です。
編集:ここでの主なポイントは、レンダリング後にスタイルを設定する必要があり、DOMに入るまでスタイルを設定してはならない複数の要素をカバーするために、ソリューションを適切にスケーリングする必要があるということです。
編集:これはトップダウンレンダリングを行う場合は問題ではありませんが、これは遅いので、下から上にレンダリングしてから、ビュー全体を一度に上に挿入できるソリューションが必要です
編集:
以下に提案するいくつかのテクニックを組み合わせて、次のようなことを考えています。コメント/批評をいただければ幸いです。
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
}