Correct AJAX practices I have got content types called 'Online Discussion', henceforth referred to as OD and a content type called 'Online Discussion Reply', henceforth referred to as ODR. Having Entity References (drupal.org/project/entityreference) to eachother.

When one ODR is created (via a front end custom form) it references its "parent" (the OD whose page it was created) and via Corresponding Entity References (drupal.org/project/cer) same thing happens the other way around.

In an OD's page, via theming, a specific amount of referenced ODRs are shown, the 10 latest. They are presented as rendered entities without any View's  or Panel's involvement at the moment but I am open to changing that if necessary. 

Each ODR has its own instance of the reply form, enhanced with quoting functionality.

Also, the ODRs can accept comments from specific roles, via a custom front-end form as well.

The requirements:

1) I need some way to load previous (all or in batches) replies with AJAX. Currently I am firing a jQuery ajax callback to a view page that fetches the previous ODRs accordingly and I inject their HTML into the DOM. That works, but in a test-case scenario with over 200 nodes referenced, that seems to take some time

2)I need a way in which after a fixed time interval to look for new ODRs and append them giving the OD a 'livelike' behavior. At the moment I do this in the same way, and it seems to work pretty ok.


3) I need some efficient way to apply Drupal Behaviors to the new elements that come to the site so they have their contextual links functional etc. I've done that using Drupal.attachBehaviors(context); but there are issues.

The issues so far:

I. Multiple instances of the same form on the same page.
Although Drupal handles drupal_html_id() on the initial OD page load, the way I use AJAX breaks the ID uniqueness. If for example I have 10 forms in my initial load, and I bring 2 more via AJAX, IDs for the first 2 will be repeated since I grab the HTML the view generated as a whole.

II. Performance issues in a 'Load all previous ODRs' case. Pretty self explanatory. Pulling 200+ entities and injecting their HTML is pretty heavy lifting.

III. Drupal.attachBehaviors - when using AJAX like the way I described, the HTML for the contextual link is produced just fine. Problem occurs with the javascript involved into it. So after some hours of researching, since there is very little documentation on this, I stumbled upon Drupal Behaviors which pretty much solved that issue, but gave me a new set of problems to deal with:


onLoad events, now in a behavior, run each time an AJAX event is fired causing multiple event binding (unbind before bind solution at the moment)
some onLoad events, like the collapse of the textareas the multiple forms use, cause usability issues. For example, you are typing a reply or comment, a new ODR comes into the site via the recurring ajax call and collapses your textarea.
pretty messy and incrementally difficult to manage code to make sure I handle all the possibilities via jQuery


So, finally, the question:

What are the best practices in a Drupal context with which to achieve the desired described result?
First of all, you have way too many questions in here. I recommend breaking this problem up into smaller chunks and trying to solve them each, rather than dumping this whole novel into stack exchange for someone to solve all your problems for you.

Maybe that sounds harsh, but there's a reason you don't have any replies yet.

I can point you toward the Drupal's once function, which will help you address at least your onLoad conundrum. (Hint: you should not be using onLoad, ever.)

In general if you're building a site in Drupal, and you can't find a module to do what you want, and you find yourself writing a lot of PHP or javascript or custom what-have-you, you might want to consider a new approach. 

There are lots of front-end frameworks that probably offer libraries to accomplish the behavior you're trying to achieve. Try finding one that comes close to solving your problem, performs well under load, and then adapt it for your use.