diff --git a/lib/assets/javascripts/err_supply-bootstrap.js b/lib/assets/javascripts/err_supply-bootstrap.js index 9d00604..93f3430 100644 --- a/lib/assets/javascripts/err_supply-bootstrap.js +++ b/lib/assets/javascripts/err_supply-bootstrap.js @@ -3,8 +3,8 @@ ErrSupply Twitter Bootstrap3 Adapter Copyright (c) 2014 Coroutine LLC Licensed under the MIT license */ -jQuery(function($) { - $("form").on("err_supply:loaded", function(event, errors) { +(function() { + var errSupplyPlugin = function(event, errors) { // define how errors are applied to dom elements var applyFn = function(error_hash) { @@ -38,7 +38,7 @@ jQuery(function($) { }; // add class - $field.addClass('error'); + $field.addClass('error').closest('.input-group').addClass('has-error'); // add popover $field.popover({ @@ -63,7 +63,7 @@ jQuery(function($) { // find all contained elements with a style of error and remove the class. // this is typically more important for ajax submissions. html submissions // tend not to have this problem. - $form.find('.error').removeClass('error'); + $form.find('.error').removeClass('error').closest('.input-group').removeClass('has-error'); // hide all fields that have been explicitly destroyed. when an html submission // has errors, the _destroy value renders as true rather than 1, which may or may not @@ -82,8 +82,13 @@ jQuery(function($) { $form.find('.error').filter(':first') : $form.find(':not(.filter) :input:visible:enabled:first'); $focus_field.focus() - - // cancel event - return false; - }); -}); \ No newline at end of file + }; + + $.fn.err_supply = function() { + $(this).on("err_supply:loaded", errSupplyPlugin.bind(this)); + } +})() + +jQuery(function($) { + $("form").err_supply(); +}); diff --git a/lib/err_supply/view_helpers.rb b/lib/err_supply/view_helpers.rb index 75e1daa..96275c2 100644 --- a/lib/err_supply/view_helpers.rb +++ b/lib/err_supply/view_helpers.rb @@ -5,11 +5,17 @@ module ViewHelpers # triggers a custom event on the associated form element. # def err_supply(obj, options={}) - id = obj.new_record? ? dom_id(obj) : dom_id(obj, :edit) + form_css_selector = options[:form_css_selector] + + if form_css_selector.blank? + id = obj.new_record? ? dom_id(obj) : dom_id(obj, :edit) + form_css_selector = "##{id}" + end + prefix = obj.class.name.underscore.split('/').last payload = err_supply_hash(obj, options.merge({ :prefix => prefix })) - "$('##{ id }').trigger('err_supply:loaded', #{ payload.to_json });".html_safe + "$('#{ form_css_selector }').trigger('err_supply:loaded', #{ payload.to_json });".html_safe end end