tags:

views:

66

answers:

1

Hey guys, I have a bit of a weird problem. I have a jquery script that basically prepends messages from data in a php script in descending chronological order one by one so that latest messages are on the top. What I want to do is somehow put a div container around two or more these messages as they come out so I decided to check which message is oldest and prepend that message with a tag and the newest message with the tag since each message comes out one by one. This however has not worked because the div does not seem to detect the enclosure and only partially encloses the 2 test messages (only the first one is enclosed). If anyone has any recommendations on how to accomplish this I would love to hear them.

     function prepare(response) {
          count++;

//function in another doc
nicetime = handleDate(response.timestamp);

if (response.replydiv=='start'){
    var list_name='<div id="chunk">start';
}
else{
    var list_name='';
}
  if (response.creator!==''){
        var list_name=list_name+'<li class="message-list-creator" id="list-'+count+'">'
    }
    else{
        var list_name=list_name+'<li class="message-list" id="list-'+count+'">'
    }




            var string = ''+list_name+''

              + '<span class="message-list-nick"><a href="statistics.php?user='+response.user+'">'+response.user+'</a></span>'
              + ' <span class="date" id='+response.timestamp+'>'+nicetime+'</span><br>'
              + '<span class="msg">'+response.message+'</span>'
              + '<span class="clear"></span>'
              if(response.reply!='null'){
               var string = string +'<a message_id="'+response.reply_chunk_id+'" id="reply">reply</a></li>';
              }
              else {
                 var string=string+'</li>';
              }


              if (response.replydiv=='end'){
            var string=string +'</div>end';
        }

          return string;
        }

$.getJSON(files+"message.php?action=view&load=initial&topic_id="+topic_id+"&username="+username+"&t=" + (new Date()), function(json) {  
            if(json.length) {
              for(i=0; i < json.length; i++) {
                  json.sort(function(a,b) { return parseFloat(a.timestamp) - parseFloat(b.timestamp) } );
                $('#message-list').prepend(prepare(json[i]));
                $('#list-' + count).fadeIn(1500);
              }

            }

          });
+2  A: 

You have a stray (and unmatched) </li> in there probably throwing your markup off, at this part:

+'</li>';

Also when you call .prepend() with the first unclosed <div> you'll have issues...it's not just adding HTML it's making DOM elements, so you need to get your entire HTML string together then .prepend() it at once.

You can optimize this much further, but the basic fixed version would be something like this:

function prepare(response) {
  count++;
  //function in another doc
  var nicetime = handleDate(response.timestamp), string="";

  if (response.div=='start'){
    string = '<div id="chunk">start div';
  }

  string += list_name;
  string += '<span class="message-list-nick">'+response.message+'</span>';

  if (response.div=='end'){
   string += '</div>end div';
  }
  return string;
}

And the ajax call:

$.getJSON(files+"script.php?action=view&load=initial&topic_id="+topic_id+"&username="+username+"&t=" + (new Date()), function(json) { 
  var output = "";
  for(i=0; i < json.length; i++) {
    json.sort(function(a,b) { return parseFloat(a.timestamp) - parseFloat(b.timestamp) } );

     output += prepare(json[i]);
     $('#list-' + count).fadeIn(1500);
  }
  $('#messagelist').prepend(output);
});

The important difference here is that we're not appending to the DOM until the string's complete...jQuery will complete the elements immediately (or attempt to) when calling .prepend() the first time.

Nick Craver
ok nick, I am kind of an idiot at this point lol. I modified the code to try to simplify it for any potential readers, so when I try your solution, modified again to fit the real code, I am getting weird results. when I switch your ajax call for example, no messages are prepended, only the div start and end...Perhaps it is due to my modifications, so I will repost my real code. If you can find it in your heart to respond again lol I would appreciate it more than you know.
Scarface
@Scarface - post the real code and I'll take a peek :)
Nick Craver
ok I just posted. Thanks a lot for your patience Nick, really appreciate it.
Scarface
@Scarface - I don't have your ajax source to test, but try something like this: http://jsfiddle.net/nick_craver/Vjjjr/ I'd still recommend building them as DOM elements rather than a concatenated string, but the link s*should* fix the immediate issue, let me know if that's not the case.
Nick Craver
I think it might work if the comments were not chronologically reversed, but they are...Could this be because the prepend is outside of the for statement? Also I noticed you using (response.creator!=='' ? '-creator' : ''). Is that like an embedded if statement? Once again, thanks for your time Nick.
Scarface
@Scarface - Use `.append()` instead of `.prepend()`, or just `.html()` if you're replacing the content.
Nick Craver
yeah I tried those on your ajax call and got the same result, that's why I am a little confused lol, sorry should have said that
Scarface
I think it has something to do with the $('#list-' + count).fadeIn(1500); since it sorted properly before, the jquery find seems to work the same regardless of whether I use append prepend html or use desc or asc mysql query. This is weird. The results just won't flip around lol. Thanks for your help once again, I will try to figure it out, let me know if you have any more ideas. Thanks again Nick.
Scarface
@Scarface - I wasn't thinking at all with the last comment, the actual problem is your sort, it's repeated which can be saved, and backwards from what you want, try this: http://jsfiddle.net/nick_craver/Vjjjr/1/
Nick Craver
O yes you are completely right! I completely overlooked that. Thank you so much Nick, really appreciate your time. Just one more question, when you did this (response.creator!=='' ? '-creator' : '') to slim that if statement down, I have never seen that before, is there any documentation I can read to learn about that format of coding? Thanks again.
Scarface
@Scarface - It's called the ternary or conditional operator, here are some good tutorial resources: http://www.google.com/search?q=javascript+ternary
Nick Craver
Thanks so much. You made my day.
Scarface
@Scarface - welcome :)
Nick Craver