Several Rails views in our application are populated through AJAX, and the first rendering of their data comes from a javascript call that fires in the jQuery event $(document.ready). The views we are interested in for this essentially show a list of tabular data (e.g., mailbox, list of search results, etc.).
We want to pre-display the first page of results without having to call AJAX, but we want to be able to re-populate the view without reloading the entire page. It's not complicated, but it seems that you need to write separate bits of code in different parts of the application that do the same thing: once in a view to draw the original data set (from the initial call render) and once more in Javascript to draw new sets data (dynamically deleting the old DOM and replacing it with new HTML).
Currently, javascript clones a hidden (c style='display: none') template element (usually div), sets the value of each field and adds a new element to the DOM. This works well, but if we want to pre-populate the view before rendering, we must write another copy of the template object in the rubyfied ERB block (for example, @messages.each do |m|). If the template is changed, then the code in the ERB block must be changed to match - not very DRY .
Is there a better way? If so, what is it?
thank
Tom
source
share