Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Poll for changes to element selects in Craft CMS
$(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