//= require selectize $(document).ready(function () { var selectizeCallback = null; $(".selectize_with_add").each(function(i, item) { $(item).selectize({ create: function(input, callback) { selectizeCallback = callback; $("." + $(item).data('model') + "-modal").modal(); $("#" + $(item).data('model') + "_name").val(input); } }); $("." + $(item).data('model') + "-modal").on("hide.bs.modal", function(e) { if (selectizeCallback != null) { selectizeCallback(); selecitzeCallback = null; } $("#new_" + $(item).data('model') + "").trigger("reset"); $("#new_" + $(item).data('model') + " select").val(null).trigger('change'); $.rails.enableFormElements($("#new_" + $(item).data('model') + "")); }); $("#new_" + $(item).data('model')).on("submit", function(e) { e.preventDefault(); $.ajax({ method: "POST", dataType: "json", url: $(this).attr("action"), data: $(this).serialize(), success: function(response) { selectizeCallback({value: response.id, text: response.name}); selectizeCallback = null; $("." + $(item).data('model') + "-modal").modal('toggle'); } }); }); }); $(".selectize_with_add_without_modal").each(function(i, item) { $(item).selectize({ create: function(input, callback) { selectizeCallback = callback; data = {}; data[$(item).data('model')] = {}; data[$(item).data('model')][$(item).data('field')] = input; $.ajax({ method: "POST", dataType: "json", url: "/" + $(item).data('pluralized'), data: data, success: function(response) { selectizeCallback({value: response.id, text: response.name}); } }); selectizeCallback = callback; } }); }); });