import wixData from 'wix-data';
let clickCount;
$w.onReady(async()=> {
let loadedClickCount = await get_clickCounter();
console.log("Loaded-Click-Counter: ", loadedClickCount);
$w('#button7').onClick(() => {console.log("Button clicked!");
clickCount++;
let newCount = loadedClickCount + clickCount;
$w('#text1').text = clickCount.toString();
saveCount(newCount);
});
});
function saveCount(newCount) {
let toSave = {
"_id": "001",
"count": newCount
};
wixData.save("myCollection", toSave)
.then((results) => {console.log(results);})
.catch((err) => {console.log(err);});
}
function get_clickCounter() {
return wixData.query("myCollection")
.find()
.then((res) => {
if (res.items.length > 0) {console.log("Click-counter found and loaded!");
let item = res.items[0];
return item.Count
}
else {console.log("No click-counter found! Nothing to load!");}
});
}