javascript - Rails 4 - event to run code after a partial is rendered -
i have js.erb file called ajax through controller action. js.erb file renders partial. inside partial data attribute generated based model. need way run code after partial rendered because code using information in data attribute.
items_controller.rb
def remove_tag @item = item.find_by_id(params[:id]) //code remove tag item @item.save respond_to |format| format.js @item.reload end end
remove_tag.js.erb
$("#taglist").html('<%= escape_javascript(render partial: "items/tag_list", locals: {item: @item}) %>'); //the code below needs run after above partial rendered $('#add-tag').rules("add", { taguniqueness: $('#taglist').data('tag-list').split(', ') });
_tag_list.html.erb
<div id="taglist" data-tag-list="<%= item.all_tags_list %>"> //more html </div>
the way now, .rules method using html data there before partial updates it.
to convert hashes or arrays ruby javascript can use .to_json
json subset of javascript.
$('#add-tag').rules("add", { taguniqueness: <%= js @badge.all_tags_list.to_json %> });
Comments
Post a Comment