Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think I see a couple issues here. I'd recommend breaking up the problem a bit since as you say you're new to jQuery. Start out using a manual button to retrieve the latest posts through AJAX. Once you've handled that then move on to the periodic updater.</p> <p>The second thing I would recommend is look into various templating methods that people use with jQuery. That is to say that you should have Ruby simply return a JSON data structure which you can then have jQuery take and populate a post template with the various pieces of data.</p> <p>Here's an example of a templating method I've used on one of my sites. I start out with the following static HTML.</p> <pre><code> &lt;table id="tblExpenses"&gt;&lt;tbody&gt; &lt;tr&gt; &lt;th&gt;Date&lt;/th&gt; &lt;th&gt;Title&lt;/th&gt; &lt;th&gt;Amount&lt;/th&gt; &lt;th&gt;Attachments&lt;/th&gt; &lt;th colspan="2"&gt;&amp;nbsp;&lt;/th&gt; &lt;/tr&gt; &lt;tr class="expenseTemplate" style="display: none;"&gt; &lt;td class="litDate"&gt;&lt;/td&gt; &lt;td class="litTitle"&gt;&lt;/td&gt; &lt;td class="litAmount"&gt;&lt;/td&gt; &lt;td class="litAttachments"&gt;test&lt;/td&gt; &lt;td&gt;&lt;a class="lnkEdit" href="#"&gt;Edit&lt;/a&gt;&lt;/td&gt; &lt;td&gt;&lt;a class="lnkDelete" href="#"&gt;Delete&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt; </code></pre> <p>I then perform an AJAX request for expenses stored in the database and dynamically create new rows to insert into the Expenses table.</p> <pre><code>function showTaxYear(taxYearId) { $.ajax({ type: "GET", contentType: "application/json; charset=utf-8", dataType: "json", url: "/Services/GetExpensesByTaxYearId", data: { taxYearId: taxYearId }, success: function(msg) { $.each(msg.Expenses, function(index, expense) { var row = $("#tblExpenses tr.expenseTemplate") .clone() .removeClass("expenseTemplate") .show() .addClass("expense") .addClass("expenseid_" + expense.ID); row.find("td.litDate").text(expense.Date); row.find("td.litTitle").text(expense.Title); row.find("td.litAmount").text(expense.Total); row.find("a.lnkEdit").attr("data-id", expense.ID) .unbind().click(function() { editExpense($(this)); }); row.find("a.lnkDelete").attr("data-id", expense.ID) .unbind().click(function() { deleteExpense($(this)); }); if (expense.Attachment != "") row.find("td.litAttachments").empty().append($("&lt;a&gt;View&lt;/a&gt;").attr("href", expense.Attachment)); else row.find("td.litAttachments").empty(); row.insertBefore("#tblExpenses tr.expenseTemplate"); }); } }); } </code></pre> <p>GetExpensesByTaxYearId returns a JSON result that looks like this</p> <pre><code>{"Expenses":[{"ID":9,"Title":"Some expense","Description":"","Date":"02/02/2010","Amount":"$10.00","Tax":0.00,"Attachment":"","Total":"$10.00"}]} </code></pre> <p>Once you've done that you can actually check to see if you've already created an element for whatever data you get back from Ruby by doing a jQuery search. In my example for instance I can check to see if an element exists with the class <code>expenseid_&lt;Identifier&gt;</code>. if it does, I've already rendered that expense.</p> <p>I know this is probably a lot to chew on but streaming AJAX content has a lot to it. Let me know if I can clarify further.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload