0

私は次のデザインを持っています

function crudDataTable()
{
    this.addEditFormContainerSelector = ''; 
    this.paginationType = "full_numbers";  // Provide Pagination Type
    this.processing = true; 
    this.serverSide = true;  //I want this this to be accessible in fnRowCallback below:

    this.create = function(){

        this.tableInstance = $(this.tableLocationSelector).DataTable({
            "sPaginationType": this.paginationType
            fnRowCallback:function(nRow, aData) {
                // "this" from crudDataTable should be accessible here
            }  
        })
    }
}

thisfromcrudDataTableをでアクセスできるようにしたいfnRowCallback

どうすればこれを達成できますか?また、コンポーネントfnRowCallbackによって起動されるため、それは私の制御ではありません。でアクセスdatatableできるようにするにはどうすればよいですか。thisfnRowCallback

または、これが不可能な場合、それを達成するための他のアプローチは何ですか?

コンポーネントを作成しようとしています。これらのコンポーネントのユーザーは、変数を設定してcreate関数を呼び出すだけです。this関数内でアクセスするというこの課題に直面していfnRowCallbackます。

ありがとう。

4

1 に答える 1

2

thisコールバックの外部への参照を保存できます。

var table = this;
this.tableInstance = $(this.tableLocationSelector).DataTable({
     "sPaginationType": this.paginationType,
     fnRowCallback: function(nRow, aData) {
         //Use `table` to refer to the instance of `crudDataTable`
     }  
});

または、ES5bindメソッドを使用することもできます(ただし、古いブラウザーではサポートされていないことに注意してください)。

this.tableInstance = $(this.tableLocationSelector).DataTable({
     "sPaginationType": this.paginationType,
     fnRowCallback: function(nRow, aData) {
         //Use `this` to refer to the instance of `crudDataTable`
     }.bind(this);
});
于 2012-06-14T14:59:43.297 に答える