javascript - Function in object is being declared as not a function -
this question has answer here:
so have created small table row generator json. when started building hadn't put in object , worked absolutley fine.
after, made abstract , needes parameters passed it. put in object re use other sets of data. however, after putting in object console message:
typeerror: this.gettablerow not function
but... function:
gettablerow: function(data) { row = "<tr>"; (i in data) { row += "<td>" + data[i] + "</td>"; } row += "</tr>"; return row; }
here full script have in context:
var datacollector = { basedataurl: 'http://tourn.dev/data/', displaydatatable: function(datapath, tableidentifier, datakeys, dataopts) { $.getjson(this.buildpath(datapath, dataopts), function (dataset) { (k in dataset) { d = dataset[k]; data = []; (i in datakeys) { data[i] = d[datakeys[i]]; } ; $(tableidentifier).append(this.gettablerow(data)); } }) }, getdataurl: function() { return this.basedataurl; }, buildpath: function(datapath, dataopts) { path = this.getdataurl(); if (datapath != null) { path += datapath; } if (dataopts != null) { if (dataopts.constructor === array) { (i in dataopts) { path += '/' + dataopts[i]; } } else { path += '/' + dataopts; } } return path; }, gettablerow: function(data) { row = "<tr>"; (i in data) { row += "<td>" + data[i] + "</td>"; } row += "</tr>"; return row; } };
this.gettablerow
not inside getjson context. store this
outside call.
displaydatatable: function(datapath, tableidentifier, datakeys, dataopts) { var self = this; // store $.getjson(this.buildpath(datapath, dataopts), function (dataset) { (k in dataset) { d = dataset[k]; data = []; (i in datakeys) { data[i] = d[datakeys[i]]; } ; $(tableidentifier).append(self.gettablerow(data)); //this in correct scope } }) }
more info scope here: http://javascriptplayground.com/blog/2012/04/javascript-variable-scope-this/
Comments
Post a Comment