Brainstorm's snippets (1/234)

  PUB/SUB in javascript

file publisher.js:

function initialize_datatable(element, url) {

    $.ajax({
        type: 'GET',
        url: url + '?action=initialize',
        dataType: 'json'
    }).done(function(data, textStatus, jqXHR) {

        ...
        var table = ...

        // Notify subscribers
        $('.subscribe-datatable-initialized').trigger(
            'datatableInitialized', [table]
        );

    });

}

file sample_subscriber.html:

<div class="panel panel-danger subscribe-datatable-initialized" id="stale-acquisition-panel" style="display: none;">

    <p>This panel is to be shown only after datatable has been initialized</p>

</div>

<script language="javascript">

    $(document).ready(function() {
        $('#stale-acquisition-panel').on('datatableInitialized', function(e, eventInfo) {
            $(e.target).show();
        });
    });

</script>