2007
//CODE
//FRONT END
//elements: 1. dynamicDataset; 2. likeButton; 3. likesText (connected to the likes field via the dataset)
import wixData from 'wix-data';
$w.onReady(function () {
$w("#dynamicDataset").onReady(() => {
let item = $w("#dynamicDataset").getCurrentItem();
$w("#likeButton").onClick((event) => {
$w("#likesText").text = (Number($w("#likesText").text) + 1).toString();
item.likesUpdate = true;
wixData.update("collectionName", item).then(() => { $w("#dynamicDataset").refresh();})
})
})
});
​
//BACK END
//filed in collection: field key: likes; Field Type: Number;
//backend/data.js
import wixData from 'wix-data';
export function collectionName_beforeUpdate(item, context) {
if (item.likesUpdate) {
return wixData.get(context.collectionName, item._id, { "suppressHooks": true })
.then(itemToUpdate => {
itemToUpdate.likes ? itemToUpdate.likes += 1 : itemToUpdate.likes = 1;
return itemToUpdate;
})
}
return item;
}
//replace the text in red by your real collection name
​