I am trying to make the javascript "constructor" call one of it's prototypical methods. Is it possible to do so?
Here is the code:
function updateableTable($table, $rowTemplate, dataObjects, getIDFromObject) // at some point add the ability to add event handlers.
{
this.$rowTemplate = $rowTemplate;
this.$tableBody = $j($table).children('tbody');
this.rowsByID = {};
this.dataObjectsByID = {};
this.getIDFromObject = {};
for(var dataObj in dataObjects)
{
addRow(dataObj);
}
return {}; // still has the protypical methods.
}
updateableTable.prototype.addRow = function(dataObject) {
//alert('inside add row');
var id = this.getIDFromObject(dataObject);
// if (true === doesIdExist(i)) {
// alert('id ' + id + ' already exists in table.');
// return false;
// }
var newRow = $rowTemplate.tmpl(dataObject);
this.rowsByID.add(id, newRow);
this.dataObjectsByID(id, dataObject);
this.$tableBody.append(newRow);
return true;
}
it references jQuery and jQuery.tmpl (just a way of converting data to the view of the data, and not really important for this question) and I call it like this:
var $j = jQuery.noConflict();
var peeps = [{ID:1, Name:'mary', Age:21},{ID:2,Name:'contrary', Age:12}];
var personIDGetter = function(person)
{
return person.ID;
}
var theTableUpdater = updateableTable($j('table.updateable'), 'hi', peeps, personIDGetter);
But it tells me (prototypical method) addRow doesn't exist.
Am I going about this all wrong?
UPDATE: Yes, I did many things wrong. I added a jsfiddle with the suggested fixes as well as a number of other fixes. It adds two rows and deletes one.