I have a page where newly created Choices are appended to the list with some ajax code, so the user don't have to refresh the entire page. The list contains of 2 columns, where the column on the right have to have a css class called "ending", which makes sure it's floated to the right and the padding is zero on the right side as well.
The problem is that when a new Choice is appended the partial which is rendered is exspecting multiple Choices if the cycle function should work.
Please see my code below and let me know if you have any suggestions to how the code should be altered so the partial inserts the correct css class.
PARTIAL:
<% @tickets.each do |choice| %>
<div id="<%= choice.id %>" class="<%= cycle("ticket shadow", "ticket shadow ending") %>" data-time="<%= choice.created_at.to_i %>">
<% end %>
AJAX in VIEW
<script type="text/javascript">
var pusher = new Pusher('<%= Pusher.key %>'); // Replace with your app key
var channel = pusher.subscribe('choices');
channel.bind('created', function(data) {
var after = $('.ticket:last').attr('data-time');
var settings = { after: after}
$.ajax({
url: '/Home/Index',
type: 'POST',
dataType: 'json',
data: settings,
success: function (data) {
$("#tickets").append(data.html);
}
});
}
);
</script>
EDIT:
I tried out the below and that seems to work, but also seems a bit hacky.. anyone with a smoother solution?
if (css_end == "ticket shadow") {
$(".ticket:last").addClass("ending");
}