replace_html or insert_html
Hello stackoverflow pals.I am a new rails learner. My question is: I am listing all items in index and also i am usin some ajax in t开发者_JS百科his page to insert new items to database. After inserting new items to database, should i get all items from database again and replace whole list of items or just add new item to list by using insert_html? Thank you.
Adding only the new items to the list is likely to be more responsive and will keep network traffic lower. However, it's also going to be a bit more complex for you as the programmer, since you'll have to figure out where to insert it and generally do more management of the table state. But either way can be a valid approach.
If you are keeping track of deletions client-side via AJAX and updating your client's view on delete as well as on insert you should not need to replace all the listings very often. It probably would make sense to poll the server periodically if multiple users are manipulating data at the same time so that one user doesn't end up with a stale view. You should probably look at a Comet implementation like Juggernaut in that case so that the server can push data to the client rather than relying on simple polling.
You also may want to consider beefing up your client-side Javascript and implement pure JSON passing between client and server. This way you only pass the data back and forth from your server to the client and the client's browser keeps the view up to date based on the JSON from the server and the logic you build with Javascript.
If you're at all worried about large amounts of traffic hitting the page, I would push as much as you can to the cilent. Do your insert and update the list accordingly.
If new items always go at the bottom and you can't sort them, I'd just append the new item to the bottom.
If you can sort them or need to insert it in a specific place, I'd re-render the whole table.
Personally, I wouldn't worry about the performance difference until it became a problem; I'd just go with the simplest thing that worked.
精彩评论