diff -u b/core/misc/states.js b/core/misc/states.js --- b/core/misc/states.js +++ b/core/misc/states.js @@ -86,30 +86,33 @@ * dependee's compliance status. */ initializeDependee: function (selector, dependeeStates) { - var self = this; + var state; // Cache for the states of this dependee. - self.values[selector] = {}; + this.values[selector] = {}; - $.each(dependeeStates, function (i, state) { - // Make sure we're not initializing this selector/state combination twice. - if ($.inArray(state, dependeeStates) === -1) { - return; - } + for (var i in dependeeStates) { + if (dependeeStates.hasOwnProperty(i)) { + state = dependeeStates[i]; + // Make sure we're not initializing this selector/state combination twice. + if ($.inArray(state, dependeeStates) === -1) { + continue; + } - state = states.State.sanitize(state); + state = states.State.sanitize(state); + + // Initialize the value of this state. + this.values[selector][state.name] = null; - // Initialize the value of this state. - self.values[selector][state.name] = null; + // Monitor state changes of the specified state for this dependee. + $(selector).bind('state:' + state, $.proxy(function (e) { + this.update(selector, state, e.value); + }, this)); - // Monitor state changes of the specified state for this dependee. - $(selector).bind('state:' + state, function (e) { - self.update(selector, state, e.value); - }); - - // Make sure the event we just bound ourselves to is actually fired. - new states.Trigger({ selector: selector, state: state }); - }); + // Make sure the event we just bound ourselves to is actually fired. + new states.Trigger({ selector: selector, state: state }); + } + } }, /** @@ -178,7 +181,7 @@ }, /** - * Evaluates child constraints to detemine if a constraint is satisfied. + * Evaluates child constraints to determine if a constraint is satisfied. * * @param constraints * A constraint object or an array of constraints. @@ -312,7 +315,6 @@ states.Trigger.prototype = { initialize: function () { - var self = this; var trigger = states.Trigger.states[this.state]; if (typeof trigger == 'function') { @@ -320,9 +322,11 @@ trigger.call(window, this.element); } else { - $.each(trigger, function (event, valueFn) { - self.defaultTrigger(event, valueFn); - }); + for (var event in trigger) { + if (trigger.hasOwnProperty(event)) { + this.defaultTrigger(event, trigger[event]); + } + } } // Mark this trigger as initialized for this element. @@ -330,23 +334,22 @@ }, defaultTrigger: function (event, valueFn) { - var self = this; var oldValue = valueFn.call(this.element); // Attach the event callback. - this.element.bind(event, function (e) { - var value = valueFn.call(self.element, e); + this.element.bind(event, $.proxy(function (e) { + var value = valueFn.call(this.element, e); // Only trigger the event if the value has actually changed. if (oldValue !== value) { - self.element.trigger({ type: 'state:' + self.state, value: value, oldValue: oldValue }); + this.element.trigger({ type: 'state:' + this.state, value: value, oldValue: oldValue }); oldValue = value; } - }); + }, this)); - states.postponed.push(function () { + states.postponed.push($.proxy(function () { // Trigger the event once for initialization purposes. - self.element.trigger({ type: 'state:' + self.state, value: oldValue, oldValue: null }); - }); + this.element.trigger({ type: 'state:' + this.state, value: oldValue, oldValue: null }); + }, this)); } };