Module-documentation

version 1.0.0
module: itsa-event
maintanance: Marco Asbreuk
home
all modules

Object emission on Event

Preferable, you emit events on an Class-instance or object, using the helpers on its prototype. However, sometimes objects are already created and you can't change the prototype. Instead of merging the listener-helpers to every single object, you should use Event.emit to emit events.

Click on the button to make "Marco 10" save.

Code-example:

<body>
    <button id="buttongo" class="pure-button pure-button-primary pure-button-bordered">Save</button>
</body>
<script src="itsabuild-min.js"></script>
<script>
    // instead of loading the data (as explained in the example-source)
    // we simulate this by just creating the array.
    // so we don't need to maintain a server running for this example
    var profiles = [],
        i;

    // create 100 profiles which all can emit through their prototype:
    for (i=0; i<100; i++) {
        profiles.push({name: 'Marco '+i});
    }

    ITSA.Event.after(
        'PersonalProfile:save',
        function(e) {
            alert(e.target.name+' got saved');
        }
    );

    ITSA.Event.after(
        'tap',
        function() {
            // we make the 11'th element to emit the save-event:
            ITSA.Event.emit(profiles[10], 'PersonalProfile:save');
        },
        '#buttongo'
    );
</script>
API Docs