Rise of the Strolenati-Ideas/Suggestions · Legacy archive
Ajax comment/vote updates
-
Ajax comment/vote updates
#1I did a neophyte ajax update of comments. Not the most beautifulist in the world, but it is a start and it seems to work.
Right now I have figure out how to return formatted html, but have not figured out how to have another response from the server with the updated vote tally and get that updated. So, while I fumble around on that, I will show the updated vote calculation with the comment the appears. That should last awhile.
It does degrade fine, if you don't have javascript enabled it works just like it used to so we should be solid.
If anybody smart in ajax wants to take a look, I am happy to get assistance. Right now I am using http://jquery.malsup.com/form/ because there is no way I write my own stuff. :) -
Re: Ajax comment/vote updates
#2Okay! This is what I've been living and breathing the past 5 months, though I've been doing it with Extjs, not with jQuery, but the formatting is the same. :D
On the API tab of that page, it mentions that the ajax form will take either a callback function, or an options object. Instead of the function that they pass in as an example, you want to specify an object, to get slightly better control over things.
Specifically, you need to change how it interprets the response you'll be sending back from the provider. Likely, the default is to just interpret it as text, which isn't what you want at all. You'll want to have the server send back a json encoded object that contains both the both information and the comment information. Your callback function can then take that returned data and push it wherever it needs to go.
So, this is roughly what you're looking for in terms of code:
var options = {
url: 'comment_provider.php',
dataType: 'json',
success: function(response) {
//dataType: 'json' means that response has already been decoded into the appropriate object, so we can access it's members directly
comment = response.comment;
vote_info = response.vote_info;
$('#comment_div').innerHTML(comment); //I think this is how jQuery handles changing a div's html. Do whatever you need to with the values here.
$('#vote_div').innerHTML(vote_info); //Same thing here.
}
};
$('#myForm').ajaxForm(options);
They also have a good example of doing all this on the website itself. My suggestion is to not echo the response like they do, but to use PHP's json_encode() method directly. That'll ensure you just have to make sure the data structures line up properly, instead of worry about whether your json encoding is correct.
If you have any questions about it, lemme know! -
Re: Ajax comment/vote updates
#3I will regale you with my ignorance. Here is the current code that makes the magic.
This is really all I have.
$(document).ready(function() {
// bind form using ajaxForm
$('#commenting').ajaxForm({
// target identifies the element(s) to update with the server response
target: '#newcomment',
url: 'http://strolen.com/commenting/ajaxpost',
type: 'post',
clearForm: true,
// success identifies the function to invoke when the server response
// has been received; here we apply a fade-in effect to the new content
success: function() {
$('#newcomment').fadeIn('slow');
}
});
});
The problem, in my head anyway and I spent about 3 hrs trying to figure it out, is that I have to do all the formatting prior to getting the data back due to the MVC of codeigniter.
In the controller I have the beast where I totally neglect the View portion of MVC just to get it working. So the script calls the comment controller which inputs the comment, requeries and gets the full comment and craps it back out in about a 80 lines that does a bunch of checks and formats it.
I guess I don't understand how to take the comment update chunk of text and the separate vote update and put them into the same json response....or do they have to be different calss?
Will try that innerHTML and see what happens. That might be fun!
Thanks -
Re: Ajax comment/vote updates
#4Chaosmark is my hero. Walked me through the ajax and got it all working nice and pretty like. -
Re: Ajax comment/vote updates
#5Hey, you're the one who got it all working nice and pretty. I just helped you understand what jQuery's ajax syntax was doing, and introduced you to json encoding. I'm a minor player at best. -
Re: Ajax comment/vote updates
#6Kudos to Chaosmark! -
Re: Ajax comment/vote updates
#7Chaos, if you can hear me....Help......Help.....
AG and others had issues with no update after post so had to remove it. Willing to run through the fields on this one again? -
Re: Ajax comment/vote updates
#8Sure, I'll take a gander at it. Running a bit of a tight schedule at the moment, but I'll see what I can figure out when I have a free moment. -
Re: Ajax comment/vote updates
#9Initial impressions:
Was javascript enabled in their browsers? Remember, AJAX calls are using javascript to do their magic, so no javascript = no post updates. This wouldn't make sense though, since the buttons would then be non-functional.
Perhaps it was a temporary fluke in server response? It may have just timed out because the server was taking a bit too long. I do know that sort of thing happened to me once in a while, which is why you always want to include a failure function as well as a success function, that way functionality is always maintained. Always make it fail on your terms, never on it's own.
Without seeing the code again, I'm mildly unequipped to debug it any further than those suggested possibilities. Also, was it just a sporatic occurrence for each person, or was there a broader timespan of non-updating? I'm trying to determine whether it's a problem with the code itself (and so every update failed), or whether it's something to do with the server response (and so it was sporatic). -
Re: Ajax comment/vote updates
#10I want to turn this on again and see what happens?
AG? You ready for a few days of annoyance to see if we got it working? -
Re: Ajax comment/vote updates
#11Turned it on tonight (rewrote it anyway). Bug reports here. :)
If it is broke, turning off javascript will allow you to post. I did absolutely nothing to the page code, should fall back gracefully. -
Re: Ajax comment/vote updates
#12I am very upset that this caused issues. Those that lost XP will regain it. I got the emails so know what you lost. Maybe a couple for your trouble!
Rise of the Strolenati-Ideas/Suggestions · All categories · Guildhall