2014-01-21 61 views
-1

之外。當我嘗試做var idx = this.dataview.getIdxById(dataContext.id);在我的init外的無功函數,我得到這個消息呼叫「這個」 INIT

Uncaught TypeError: Cannot call method 'getIdxById' of undefined

。我怎樣才能訪問這個?這僅僅是基本骨架(是的CustomFormatter相隔一列定義的):

function($) { 
    /** 
    * @class test.test.testing 
    */ 

    /** @Static */ 
    { 
     defaults : { 
    columns: [{id: "hello", 
         name: "hello", 
         field: "hello", 
         width: 150, 
         sortable: true, 
         formatter: customFormatter},], 
     } 
    }, 
    /** @Prototype */ 
    { 
    init : function() { 
      this._super(); //the grid 
     } 
    }); 
}); 

var customFormatter = function (row, cell, value, columnDef, dataContext) { 
    var idx = this.dataview.getIdxById(dataContext.id); 
}; 
+0

呃,我們是否應該猜測'customFormatter'函數被調用的範圍,或者它應該調用的範圍?使用'apply()'或'call()'設置正確的範圍可能是答案,但誰知道在哪裏? – adeneo

+0

格式。說明。 – marekful

+0

剛剛編輯...... –

回答

0

您沒有提供了足夠的細節,但我的猜測是,這個變量沒有被設置爲你想要的。實際上,這是不確定的。嘗試將代碼更改爲以下代碼:

var customFormatter = function (me, row, cell, value, columnDef, dataContext) { 
    var idx = me.dataview.getIdxById(dataContext.id); 
}; 
// adding argument for this, and then call: 
customFormatter(this, row, cell, value, columnDef, dataContext); 
var customFormatter = function (me, row, cell, value, columnDef, dataContext) { 
    var idx = me.dataview.getIdxById(dataContext.id); 
}; 
// adding argument for this, and then call: 
customFormatter(this, row, cell, value, columnDef, dataContext); 
+0

'this'不可能是未定義的,但'this.dataview'是。我在'customFormatter'的上下文中猜測'this'是指窗口,而不是他們期望的。 –

+0

我剛編輯過來向你展示customFormatter在哪裏 –