バックグラウンド
作成したjQueryプラグインがあります。プラグインはページ上のすべてのテーブルを取得し、リモートリクエストを行うことで行を読み込みます。
コード例
これは、プラグインで使用される構造化の高さを示す簡単な例です。実際の例では、load関数がAJAXリクエストを作成し、その結果を処理してテーブルに行を追加します。
JavaScript
(function($) {
$.table = function(el, options) {
// To avoid scope issues, use 'base' instead of 'this'
// to reference this class from internal events and functions.
var base = this;
// Access to jQuery and DOM versions of element
base.$el = $(el);
base.el = el;
base.load = function() {
base.$el.find('tbody:last').append("<tr><td>...</td><td>...</td></tr>");
};
base.load();
};
$.fn.table = function(options) {
options = options || {};
return this.find(options.selector)
.each(function(index) {
new $.table(this, options);
})
};
})(jQuery);;
HTML
<table id="table1" data-role="table">
<thead id="tour-table">
<tr>
<th data-id="id" width="20px">col1</th>
<th data-id="given_name">col2</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<table id="table2" data-role="table">
<thead id="tour-table">
<tr>
<th data-id="id" width="20px">col1</th>
<th data-id="given_name">col2</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
このようなページのすべてのテーブルを初期化します。
$('body').table({selector:'[data-role="table"]'});
jsfiddle
実際の例を見ることができますhttp://jsfiddle.net/RWy5r/1/
質問
load関数をdomに公開して、このようなオブジェクトに対して呼び出すことができるようにすることは可能ですか?
$("#table1").load();
また
$("#table1").table.load();
上記の簡単な例を使用すると、ID "table1"のテーブルが<td>
追加され、ID"table2"のテーブルを含む他のすべてのテーブルは変更されないままになります。