I'm trying to create a simple JS class and then trying to create an object but in the marked row it says "Cannot read property 'show' of undefined".
function DashboardNumberItem(dashboardItemId, dataUrl) {
this.dashBoardItem = $("#" + dashboardItemId);
this.dataurl = dataUrl;
this.placeholder = $("#" + dashboardItemId + " .number-placeholder");
this.dashBoardItem.hide();
}
DashboardNumberItem.prototype.loadData = function() {
$.ajax({
url: this.dataurl,
method: 'GET',
dataType: 'json',
success : function(data) {
this.dashBoardItem.show(); // this.dashBoardItem is undefined?!
this.placeholder.text(data);
}
});
};
var dashboardItem = new DashboardNumberItem("dashboard-item-vardhandelseimportcount", "/Controller/Action");
dashboardItem.loadData();
Why does this happen and how can I make it work?