Here's the code as it is now, promise chain is the same it was before. I've started to attempt to use Promise.all() to move away from the antiPattern
(function(){
'use strict';
function DOMObj(){
var that = this;
that.items = [];
that.getitems = function(url) {
return new Promise (resolve => {
$.getJSON(url, function (response) {
for (var i = 0; i < response.sales.length; i++) {
that.items.push(new ProductObj(response.sales[i], i));
}
resolve();
});
}
)
};
that.updateProductHTML = function(){
return new Promise(resolve => {
for( var i = 0; i < that.items.length; i++){
that.items[i].updateHTML();
}
resolve();
})
};
that.updateDOM = function() {
return new Promise(resolve => {
var thisHTML = '';
for( var i = 0; i < that.items.length; i++) {
if (i % 3 === 0 ) {
thisHTML += "<div class='row'>";
// console.log("START");
}
thisHTML += that.items[i].htmlView;
if ((i % 3 === 2) || i === (that.items.length - 1) ) {
thisHTML += "</div>";
console.log("FINISH");
}
}
$("#content").append(thisHTML);
resolve();
})
}
}
function ProductObj(product, i) {
var that = this;
that.photo = product.photo;
that.title = product.title;
that.subtitle = product.subTitle;
that.url = product.url;
that.htmlView = "";
that.index = i;
that.updateHTML = function() {
$.get('template.html', function(template){
that.htmlView = template.replace('{image}', that.photo)
.replace('{title}', that.title)
.replace('{subtitle}', that.subtitle)
.replace('{url}', that.url);
console.log(that.index + ' product has worked through html')
})
};
}
var myView = new DOMObj();
myView.getitems('data.json')
.then(
myView.updateProductHTML
)
.then(
myView.updateDOM()
)
.then(() =>{
$('.deleteButton').click(() => {
$(this).parent().remove();
});
//Promise.all([ myView.getitems('data.json'), myView.updateProductHTML, myView.updateDOM, () => {$('.deleteButton').click(() => {
//$(this).parent().remove();
//})}])
})();
so far the code runs in this order getItems => updateProductHTML
then updateDOM
runs with it, the last code I'm attempting to add is a click event on a button which needs to run last apparently