Created
June 20, 2018 12:14
-
-
Save JustAnotherMark/41a03a6c3866c28012ac1e7eba25ab40 to your computer and use it in GitHub Desktop.
Reacting to Drupal 8 JavaScript #states events
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 }); | |
| } | |
| } | |
| }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $('.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