I need assistance with the code snippet provided below.
My goal is for the 'airported' object to generate a list of airports. However, I am struggling to retrieve the data once the each loop has completed its iteration in finding 'li's in the list.html file (retrieved via ajax).
The airported object utilizes an instance of the Airport class. I prefer not to have complex code scattered throughout my application. All I want is to invoke 'airport.list()' and receive the desired list of airports.
The list.html file is included for reference purposes only. There is no need to extensively analyze it. Despite this, I must utilize it and cannot opt for a clean JSON format or any similar alternative.
Javascript
function Airport() {
this.list = function() {
var airportList = []
var promise = this.data();
promise.success(function(data){
var list = data;
var listSize = $(data).find('li').size();
$.each($(list).find("li"), function(key,val) {
airportList.push(val);
if(listSize == key+1) {
console.log(airportList);
return false;
}
});
})
return airportList;
};
this.data = function() {
return $.ajax({
url: "/list.html"
})
};
}
var airported = new Airport();
console.log('airported', airported.list());
list.html
<html>
<head></head>
<body>
<div id="listautocomp" style="background:white">
<ul id="ulSuggest">
[Airport list items omitted for brevity]
</ul>
</div>
</body>
</html>