My ajax response is correct but i am unable to return the response object back to my method call. The problem lies in the first part of the if statement. This is my first project with attempts at OOP. Please let me know how to fix this and any other OOP code suggestions are very welcome.
$("#search-btn").on('click', function(e) {
e.preventDefault();
searchTerm = $("#search-input").val();
var params = {'q': searchTerm, 'limit': 13};
var url = "https://api.twitch.tv/kraken/search/" + searchType + "?";
/*===*/ if(searchType === "games") {
url = "https://api.twitch.tv/kraken/search/games?type=suggest";
var gamesSearch = new TwitchApiCall(searchType, searchTerm, params, url);
var data = gamesSearch.apiCall();//How do i get response from api call here?=====
console.log(data);//returns undefined================ /*===*/
//will change these when above works=====================
} else if (!searchType || searchType === "channels") {
var defaultSearch = new TwitchApiCall(searchType, searchTerm, params, url);
defaultSearch.apiCall();
displaySearchResults(defaultSearch.response.channels);
} else {
var streamSearch = new TwitchApiCall(searchType, searchTerm, params, url);
streamSearch.apiCall();
displaySearchResults(streamSearch.response.streams);
}
});
}
function TwitchApiCall(searchType, searchTerm, params, url) {
this.params = params;
this.url = url;
this.apiCall = function() {
$.ajax({
url: this.url,
data: this.params,
/*===*/ success: function (response) {
next = response._links.next;
prev = response._links.prev;
console.log(response);//returns the object i want==================
}
}); /*===*/
$("#page-title").html("Twitch.tv/" + searchType + "/" + searchTerm);
$("#sort-pop").addClass('active');
};
}
Thanks in advance