Skip to content

Instantly share code, notes, and snippets.

@JustAnotherMark
Created June 20, 2018 12:14
Show Gist options
  • Select an option

  • Save JustAnotherMark/41a03a6c3866c28012ac1e7eba25ab40 to your computer and use it in GitHub Desktop.

Select an option

Save JustAnotherMark/41a03a6c3866c28012ac1e7eba25ab40 to your computer and use it in GitHub Desktop.
Reacting to Drupal 8 JavaScript #states events
// Sample of core/misc/states.js starting at line 64 (currently).
// console.log() can be used to find the name of events being triggered by Drupal states.
states.Dependent.prototype = {
initializeDependee: function initializeDependee(selector, dependeeStates) {
var state = void 0;
var self = this;
function stateEventHandler(e) {
// console.log(e.data.state) will output the state whenever it is triggered.
self.update(e.data.selector, e.data.state, e.value);
}
this.values[selector] = {};
for (var i in dependeeStates) {
if (dependeeStates.hasOwnProperty(i)) {
state = dependeeStates[i];
if ($.inArray(state, dependeeStates) === -1) {
continue;
}
state = states.State.sanitize(state);
this.values[selector][state.name] = null;
// console.log('state:' + state) will output the full event name during setup.
$(selector).on('state:' + state, { selector: selector, state: state }, stateEventHandler);
new states.Trigger({ selector: selector, state: state });
}
}
},
$('.whatever-selector').on('state:checked', function(e) {
// Do something.
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment