これはいつも私を取得します。Web ページ上の素敵な UI 要素をすべて初期化した後、一部のコンテンツを (モーダルまたはタブなどに) 読み込みますが、新しく読み込まれたコンテンツには UI 要素が初期化されていません。例えば:
$('a.button').button(); // jquery ui button as an example
$('select').chosen(); // chosen ui as another example
$('#content').load('/uri'); // content is not styled :(
私の現在のアプローチは、バインドが必要な要素のレジストリを作成することです。
var uiRegistry = {
registry: [],
push: function (func) { this.registry.push(func) },
apply: function (scope) {
$.each(uiRegistry.registry, function (i, func) {
func(scope);
});
}
};
uiRegistry.push(function (scope) {
$('a.button', scope).button();
$('select', scope).chosen();
});
uiRegistry.apply('body'); // content gets styled as per usual
$('#content').load('/uri', function () {
uiRegistry.apply($(this)); // content gets styled :)
});
この問題を抱えているのは私だけではないので、これを行うためのより良いパターンはありますか?