Listening For Delegate Events

To listen for any events in your widget, you can use the bind() method on the delegate element. Let's listen for the fileselected.module_assets event we listened for:

Recite.Delegate.bind(
    'fileselected.module_assets', 
    function(e, memo) {
        Recite_Dialog.Alert({
            msg : 'File with ID ' + memo.id + ' was selected'
        });
    }
);

Note, however, that there is a fundamental flaw in this code: we need to unbind this listener when the widget is destroyed, and this code doesn't allow us to do this easily.

You could call Recite.Delegate.unbind('fileselected.module_assets'), however, this would mean every other widget listening for this event would also be unbound.

To deal with this, you must either pass the function as the second argument to unbind() (meaning you can't use an anonymous function like in our example), or you can just use the Recite_Tabs.Delegate() method to originally bind the event.

Recite_Tabs.Delegate(
    widget,
    'fileselected.module_assets', 
    function(e, memo) {
        Recite_Dialog.Alert({
            msg : 'File with ID ' + memo.id + ' was selected'
        });
    }
);

Binding the event in this manner means it will automatically be unbound when the widget is destroyed.

Note

You must pass the widget as the first argument to Delegate().