Post by baratheon on Jan 5, 2014 10:43:36 GMT -8
I'm working on a raffle plugin and I'm experiencing an odd issue with writing to a key. For the raffle, I'm storing a JSON string of all participating members along with the number of entries they've submitted, and I add additional info to the JSON whenever a user submits an entry. The problem I have is that, when a user is submitting an entry, and the page wasn't refreshed to load the most recent key data, the incoming entry will override the data that hasn't yet been loaded. For instance, if my friend and I are both on the raffle page, and I submit 3 entries, the entries he will submit after me will override my 3 entries resulting in ONLY - friend: # of entries (that is, unless he refreshed the page); when in fact it should be - me: 3, friend: # of entries. Any ideas on how to avoid this behavior?
Here is a link to the plugin (on the front page): www.testforum1995.freeforums.net/ although you may not be able to see it unless you are logged in.
Here's the relevant code:
Here is a link to the plugin (on the front page): www.testforum1995.freeforums.net/ although you may not be able to see it unless you are logged in.
Here's the relevant code:
var current_entries = 0;
if(pb.plugin.key('all_entries').get() != undefined && pb.plugin.key('all_entries').get() != ''){
var get_all_entries = pb.plugin.key('all_entries').get();
var all_entriesObj = $.parseJSON(get_all_entries);
if(all_entriesObj[pb.data('user').name]){
current_entries = all_entriesObj[pb.data('user').name];
}
}
$('#users-current-entries').html(current_entries);
//activate submit button
$('#entry-btn').click(function(){
var entry_input = $('#entry-txt').val();
if(entry_input.match(/^[0-9]+$/) != null){
var entry_limit = pb.plugin.get('raffle_center').settings.entry_limit;
if(entry_limit == undefined){
entry_limit == 0;
}
var new_entries = parseInt(entry_input) + parseInt(current_entries);
if(new_entries <= entry_limit || entry_limit == 0){
//if(pixeldepth.monetary.get() >= (entry_input*entry_cost)){
$('#entry-error-msg').html('Entry submitted successfully!');
$('#entry-txt').val('');
//insert user's name and current_entries into all_entries
var get_all_entries = pb.plugin.key('all_entries').get();
var user_name = user.name;
var appendObj = {};
appendObj[user_name] = new_entries;
var all_entriesObj = $.parseJSON(get_all_entries);
var combine_entries = $.extend(all_entriesObj, appendObj);
var combine_entriesString = JSON.stringify(combine_entries);
current_entries = combine_entries[user_name];
$('#users-current-entries').html(current_entries);
alert(get_all_entries);
pb.plugin.key('all_entries').set({value: combine_entriesString});
updateParticipants();
}else{
$('#entry-error-msg').html('Entry too large! Maximum number of entries allowed is ' + entry_limit);
}
}else{
$('#entry-error-msg').html('Only numbers without spaces allowed!');
}
});