I have created a Javascript object that adds a click listener onto certain classes which then in turn calls a PHP function to generate results from a MYSQL/PHP driven backend. The backend is working fine (and the results are being returned correctly). I am having an issue where the instance thinks that a property (a string) of the instance is empty, even after I have directly assigned values to that property.
Here is my code:
//Workout Videos Class
var Workout = function(offset, limit, element, categoryId) {
this.offset = offset;
this.html = "";
this.limit = limit;
this.element = element;
this.categoryId = categoryId
var self = this;
jQuery(this.element).on("click", function(e) {
e.preventDefault();
self.seeMoreClick(this, self.videoCallBack, self)
});
}
Workout.prototype.seeMoreClick = function(item, videoCallBack, thisArg) {
jQuery.ajax({
type : 'POST',
dataType : 'json',
url : 'api/get-videos',
data : 'category_id=' + this.categoryId + "&offset=" + this.offset + "&limit=" + this.limit,
success : function(data) {
videoCallBack.call(thisArg, data, item);
}
});
}
Workout.prototype.videoCallBack = function(response, item) {
this.offset = response.offset;
if(response.videos) {
jQuery(response.videos).each(function(key, video) {
this.html += "<div class='col-md-4' data-related-video='" + video.videoId + "'>";
this.html += '<a href="#" class="dashboard-video"></a>';
this.html += '<div class="video-information">';
this.html += '<h5 class="tk-bebas-neue">' + video.videoName + '</h5>';
this.html += '<p>' + video.videoDesc + '</p>';
this.html += '<p>Equipment: ';
jQuery(video.equipment).each(function(equipKey, equipmentItem) {
this.html += '<a href="#" class="equipment-item" data-equipment-id="' + equipmentItem.equipmentId + '">' + equipmentItem.equipmentName + '</a>';
});
this.html += "</p>";
this.html += '<div class="video-rating" data-rel="' + video.videoId + '" data-user-id="' + video.userId + '">';
this.html += "</div>";
this.html += '</div>';
this.html += "</div>";
});
} else {
this.html += "<div class='col-md-12'>No more videos to show</div>";
}
console.log(this.html);
jQuery("#" + jQuery(this.element).attr("data-related") + " .col-md-8").append(this.html);
}
The problem I am having is in the 'videoCallBack' function which I am using to pass to the AJAX function in order to ensure it resolves before all the code is run. The 'this.html' property is showing as empty when I log it in the console directly after setting it. Can anyone see why this may be happening? Thanks