-1

I get this message

Uncaught TypeError: Cannot call method 'getIdxById' of undefined

When I try to do var idx = this.dataview.getIdxById(dataContext.id); in a var function outside my init. How can I access this? This is just the basic skeleton (customFormatter is apart of a column definition):

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);
};
Abbas
  • 14,186
  • 6
  • 41
  • 72
Doc Holiday
  • 9,928
  • 32
  • 98
  • 151
  • Uhm, are we supposed to guess what scope the `customFormatter` function is called in, or even what scope it's supposed to be called in? Using `apply()` or `call()` to set the right scope is probably the answer, but who knows where? – adeneo Jan 21 '14 at 15:16
  • 1
    You need to look at the proper way to use `this` try reading this: http://yehudakatz.com/2011/08/11/understanding-javascript-function-invocation-and-this/ – Adjit Jan 21 '14 at 15:30

1 Answers1

0

You haven't provided enough details, but my guess is that the this variable is not set to what you want. Aparently this is undefined right now. Try changing the code to the following:

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);
user1213898
  • 173
  • 7
  • It's unlikely that `this` is undefined, but `this.dataview` is. I'd guess in the context of `customFormatter` that `this` refers to the window, rather than whatever they expect it to. – Anthony Grist Jan 21 '14 at 15:21