Object method as jqGrid callback
I have an object with a method that i want to be a callback in grid. How can i do it?
Non-working sample code:
var GridHolder = function() {
//...
this.lastSel = null;
//...
};
GridHolder.prototype = {
//...
someAction : function(id){ /*doSomeWork();*/},
rowSelect : function(id){
this.someAction(id); // failed
if(id && id !== this.lastSel){
jQuery('#g开发者_开发知识库rid_id').restoreRow(this.lastSel);
this.lastSel=id;
}
jQuery('#grid_id').editRow(id, true);
}
//...
};
var gridHolder = new GridHolder();
jQuery('#grid_id').jqGrid({
//...
onSelectRow: gridHolder.rowSelect,
//...
});
For now callbacks are called by jqGrid with 'call' and that is substitute 'this' in methods.
I.e.:
if( $t.p.onSelectRow && onsr) { $t.p.onSelectRow.call($t, pt.id , stat); } // this === $t in callback instead of gridHolder
var gridHolder = new GridHolder();
jQuery('#grid_id').jqGrid({
//...
onSelectRow: function(id) { gridHolder.rowSelect(id); },
//...
});
should execute in the correct context.
精彩评论