首页 > 代码库 > flexigrid的处理函数使用闭包传递this对象

flexigrid的处理函数使用闭包传递this对象

很多时候,process函数都需要获得this对象,然后调用一些方法。

下面举个例子:

	config : function (groupName, description, deviceNumber, del) {
	    var ob = this;
	    $("#groups").flexigrid(
		{
		    dataType: ‘json‘,
		    width: 870,
		    height: 300,
		    colModel : [
			{
			    display: groupName,
			    name: ‘groupName‘,
			    width: 200,
			    sortable: true,
			    align: ‘left‘,
			    process: (function (ob, hdl) {
				return function (celDiv, id) {
				    hdl(ob, celDiv, id);
				};
			    }(ob, ob.editName))
			},
			{
			    display: description,
			    name: ‘description‘,
			    width: 469,
			    sortable: true,
			    align: ‘left‘,
			    process: (function (ob, hdl) {
				return function (celDiv, id) {
				    hdl(ob, celDiv, id);
				};
			    }(ob, ob.editDescription))
			},
			{
			    display: deviceNumber,
			    name: ‘deviceNumber‘,
			    width: 110,
			    sortable: true,
			    align: ‘left‘
			},
			{
			    display: del,
			    name: ‘del‘,
			    width: 25,
			    sortable: true,
			    align: ‘left‘,
			    process: (function (ob, hdl) {
				return function (celDiv, id) {
				    hdl(ob, celDiv, id);
				};
			    }(ob, ob.remove))
			}
		    ]
		}
	    );
	    $.ajaxSetup({ cache: false });
	},


这样就能完美的解决无法传额外参数的问题。



flexigrid的处理函数使用闭包传递this对象