diff --git a/misc/states.js b/misc/states.js index ec070c9..00c8a89 100644 --- a/misc/states.js +++ b/misc/states.js @@ -18,11 +18,7 @@ Drupal.behaviors.states = { attach: function (context, settings) { for (var selector in settings.states) { for (var state in settings.states[selector]) { - new states.Dependent({ - element: $(selector), - state: states.State.sanitize(state), - dependees: settings.states[selector][state] - }); + states.CreateState(selector, state, settings); } } @@ -34,6 +30,33 @@ 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 (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; + new states.Dependent({ + element: $(selector), + state: states.State.sanitize(state), + dependees: settings.states[selector][state] + }); + } +}; + +/** * Object representing an element that depends on other elements. * * @param args