I created an online community in Drupal with a homepage that looks like a Facebook wall. You see the 25 most recent posts with the last two comments below those posts. There is also a text box right below these comments so you can quickly post a new comment on a specific post.
These Facebook-style posts have many features built into them via JavaScript. By clicking the "view all comments" link directly below the message, you get an AJAX call that captures all the comments for that message and displays them directly below it. You can also mark posts as useful, as a solution to your question, edit inline comments, etc. All these actions require AJAX requests, which means that the JavaScript executing the request needs to know important information such as Node identifier (unique identifier for the message), comment identifier (unique comment identifier), etc.
In my initial implementation, these pieces of important data were scattered across all posts, which complicated the JS record that was supposed to find it. Thus, my second implementation simply prints all this data in a JSON-compatible string in the main packaging element of each message. Although this greatly simplified JS to find the necessary data, writing JSON as a string is a pain (slipping quotes, no line breaks).
So now I have a third idea, and I am looking for feedback on this before its implementation. The idea is to create a single global JS array for all these messages, which contains objects inside it that store data for each message. Each element in this array will contain the necessary data needed for AJAX calls. Therefore, it looks something like this:
Facebook-style email philosophy
<div class="post" data-postindex="<?php echo $post->index; ?>"> </div> <script type="text/javascript"> globalPostArray.push({ nid: <?php echo $post->nid; ?>, authorID: <?php $post->authorID; ?>, //etc. etc. etc. }); </script>
The result of the above code is that when you click on a link that requires an AJAX request, JS simply crosses the DOM up from that link until it finds the main .post element. Then it will capture the value of data-postindex to find out which element in globalPostArray contains the data it needs.
Thoughts? I feel that there must be some standard, acceptable way to achieve something like this.