I am just starting to learn the Modular Design principles and I believe I am not understanding the context of one of my methods. In the console, I am getting Uncaught TypeError: Cannot read property 'val' of undefined - line 19
. I am using Firebase, if that matters.
Here is my code:
(function(){
var UpdateTable = {
resources: Resources,
init: function(){
this.cacheDom();
this.render();
this.update(snapshot); // Changed this line ///
},
render: function(){
this.resources.on('value', this.update);
},
cacheDom: function(){
this.$table = $('#resourceTable');
},
update: function(snapshot){
console.log(snapshot.val());
for(var resource in this.resources){
this.$table.append('<tr><td>' + this.resources[resource].name + '</td><td>' + this.resources[resource].language + '</td></tr>');
}
}
};
UpdateTable.init();
})();
If you want to make this truly modular, I suggest to pass snapshot
as a parameter for your module. This will fix your issue.
(function(snapshot){ // <----- Here I receive snapshot from the invoker
var UpdateTable = {
resources: Resources,
init: function(){
this.cacheDom();
this.render();
this.update(snapshot); // Changed this line ///
},
render: function(){
this.resources.on('value', this.update);
},
cacheDom: function(){
this.$table = $('#resourceTable');
},
update: function(snapshot){
console.log(snapshot.val());
for(var resource in this.resources){
this.$table.append('<tr><td>' + this.resources[resource].name + '</td><td>' + this.resources[resource].language + '</td></tr>');
}
}
};
UpdateTable.init();
})(snapshot); // <---- Here I am passing snapshot from global context to the module context