diff --git a/misc/states.js b/misc/states.js index 594f818..90f1be3 100644 --- a/misc/states.js +++ b/misc/states.js @@ -16,14 +16,9 @@ var states = Drupal.states = { */ Drupal.behaviors.states = { attach: function (context, settings) { - var $context = $(context); for (var selector in settings.states) { for (var state in settings.states[selector]) { - new states.Dependent({ - element: $context.find(selector), - state: states.State.sanitize(state), - constraints: settings.states[selector][state] - }); + states.CreateState(context, selector, state, settings); } } @@ -35,6 +30,34 @@ Drupal.behaviors.states = { }; /** + * Check needs to be performed if states object exists for a given + * selector and state combination. If this is not done, multiple states + * are created when used in combination with ajax. This results + * in wrong evaluation of states. + */ +states.CreateState = function (context, selector, state, settings) { + if (typeof states.CreateState.states == 'undefined') { + states.CreateState.states = new Array(); + } + + if (typeof states.CreateState.states[selector] == 'undefined' || + typeof states.CreateState.states[selector][state] == 'undefined') { + //no states object was found for given selector-state combination. create one + if (typeof states.CreateState.states[selector] == 'undefined') { + states.CreateState.states[selector] = new Array(); + } + //set the flag for given selector-state combination to true that we don't create duplicate objects + states.CreateState.states[selector][state] = true; + var $context = $(context); + new states.Dependent({ + element: $context.find(selector), + state: states.State.sanitize(state), + constraints: settings.states[selector][state] + }); + } +}; + +/** * Object representing an element that depends on other elements. * * @param args