views:

77

answers:

3

This is a situation I've run into several times, not sure what the best approach is.

Let's say I have some kind of dynamic content that users can add to via Ajax. For example, take Stack Overflow's comments - people can add comments directly on to the page using Ajax.

The question is, how do I implement the addition itself of the new content. For example, it looks like the ajax call which Stack Overflow uses to add a comment, simply returns html which replaces all of the comments (not just the new one). This way saves redundant logic in the Javascript code which has to "know" what the html looks like.

Is this the best approach? If so, how best to implement it in Django so that there is no redundancy (i.e., so that the view doesn't have to know what the html looks like?)

Thanks!

EDIT: In my specific case, there is no danger of other comments being added in the meantime - this is purely a question of the best implementation.

+1  A: 

The reason to update all of the comments is to take in account other comments that other people may have also submitted in the meantime. to keep the site truly dynamic, you can do either that, or even, when the page is loaded, load up a variable with the newest submitted comment ID, and set a timer that goes back to check if there are more comments since then. if there are, return them as a JSON object and append them onto the current page a DIV at a time. That would be my preferred way to handle it, because you can then target actions based on each DIV's id or rel, which directly relates back to the comment's ID in the database...

FatherStorm
Thasnk - you make a good point about the reason for updating. I've updated the question, since that specific reason is not important to me right now.
Edan Maor
+1  A: 

I am not proficient neither with Django nor Python but I suppose the basic logic is similar for all server-side languages.

When weighing the pros and cons of either approach things depend on what you want to optimize. If bandwidth is important then obviously using pure JSON in communication reduces latency when compared to transmitting ready-made HTML.

However, duplicating the server-side view functionality to Javascript is a tedious and error-prone process. It simply takes a lot of time.

I personally feel that in most cases (for small and medium traffic sites) it's perfectly fine to put the HTML fragment together on server side and simply replace the contents of a container (e.g. contents of a div) inside an AJAX callback function. Of course, replacing the whole page would be silly, hence your Django application needs to support outputting specific regions of the document.

Saul
+3  A: 

If the content is simple, I would get JSON and build the HTML in jQuery. If it's complex, I would create a template and call render() on it on the server and return the HTML (which jQuery could either append to other content or replace existing content).

Hollister
Thanks - that's what I ended up doing (using render_to_string to render specific snippets.)
Edan Maor
Great! So you're accepting this answer?
Hollister
A jQuery plugin I always tout is taconite. (http://jquery.malsup.com/taconite/) It can allow for complex AJAX/Django/browser interaction without requiring hardly any neurons at all. :-)
Peter Rowell