Post by Anatoly03 on Sept 7, 2019 8:53:19 GMT -8
Forum URL: area26.boards.net
So I'm currently working and trying to get into plugins and I already read through the PB API, currently working on a votes system (to get into plugins).
At this point, I have a fully working UI redesign for the votes container (using jQuery, I assume that's the only way to do so).
For the votings I have two buttons: Up- and Downvotes. How do I call an event upon their clicks, which will manipulate the key?
The Ui (first cut in above code) is generated like this:
For the vote buttons, I have already tried putting an onclick="vote(0)", and then just calling the function vote(), which was located above: It didn't work.
How do I call a function in the plugin upon button click (and preferably make it safe against invaders)?
LATEST EDIT:
Doesn't work either anymore after having reloaded the page. Sadly there are not as many examples on how to write plugins with keys.
So I'm currently working and trying to get into plugins and I already read through the PB API, currently working on a votes system (to get into plugins).
At this point, I have a fully working UI redesign for the votes container (using jQuery, I assume that's the only way to do so).
For the votings I have two buttons: Up- and Downvotes. How do I call an event upon their clicks, which will manipulate the key?
// Topics/Posts Manipulator
$(function() {
let plugin = pb.plugin.get('user_reputation');
$('div.posts div.content table.list tr.post').each( function () {
let post_id = +$(this).attr('id').substring(5, $(this).attr('id').length); // [GET POST ID: WORKS]
let votes = 0;
if (pb.plugin.key('vote').get(post_id)) {
let arr = JSON.parse(pb.plugin.key('vote').get(post_id));
arr.foreach(function(el) {
votes += el.value;
});
} else {
pb.plugin.key('vote').set({ object_id: post_id, value: '[]' });
}
/* [CUT: JQUERY MANIPULATES UI HERE; THIS PART WORKS VERY WELL] */
});
/* [CUT] */
});
The Ui (first cut in above code) is generated like this:
$('div.mini-profile', this).append(
'<div class="vote-area"><table><tr>'
+ '<td width="20%"><a class="vote-button"><span class="vote-button">▲</span></a></td>'
+ '<td class="votes-count" width="60%">' + votes + '</td>'
+ '<td width="20%"><a class="vote-button"><span class="vote-button">▼</span></a></td>'
+ '</tr></table></div><br>'
);
For the vote buttons, I have already tried putting an onclick="vote(0)", and then just calling the function vote(), which was located above: It didn't work.
How do I call a function in the plugin upon button click (and preferably make it safe against invaders)?
LATEST EDIT:
let votes = 0;
if (pb.plugin.key('vote').get(post_id)) {
let arr = JSON.parse(pb.plugin.key('vote').get(post_id));
arr.foreach(function(el) {
votes += el.value;
});
} else {
pb.plugin.key('vote').set({ object_id: post_id, value: '[]' });
}
Doesn't work either anymore after having reloaded the page. Sadly there are not as many examples on how to write plugins with keys.