Created
          September 24, 2015 08:28 
        
      - 
      
- 
        Save mmikkel/e24e379faad6fd3fbde5 to your computer and use it in GitHub Desktop. 
    Poll for changes to element selects in Craft CMS
  
        
  
    
      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
    
  
  
    
  | $(function () { | |
| // Bind a greedy event handler | |
| $('body').on('elementselectchange', '.elementselect', onElementSelectChange); | |
| // Poll to evalute elementselects every 10ms | |
| setInterval(evaluateElementSelects, 10); | |
| function evaluateElementSelects () | |
| { | |
| var elementSelect, | |
| elementIds; | |
| $('.elementselect').each(function () { | |
| elementSelect = $(this).data('elementSelect'); | |
| elementIds = $.map(elementSelect.$elementsContainer.children(), function (element) { | |
| return $(element).data('id'); | |
| }).join('|'); | |
| if (elementIds !== elementSelect.$container.data('elementIds')) { | |
| elementSelect | |
| .$container | |
| .data('elementIds', elementIds) | |
| .trigger('elementselectchange'); | |
| } | |
| }); | |
| } | |
| function onElementSelectChange (e) | |
| { | |
| var $elementSelect = $(e.currentTarget); | |
| console.log('element select changed in some way', $elementSelect, $elementSelect.data('elementIds')); | |
| } | |
| }); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment