matthewatabet matthewatabet - 1 year ago 61
Javascript Question

How do I check for the existence of a backone callback?

Let's say I've installed a callback on my backbone model like so:

model.on('some_event', () => {
console.log('it happened.');

Is there a way to check that this callback is installed? My goal is to avoid installing the callback twice.

Answer Source

You can check to see if your model has an event registered with the following:

if(model._events && model._events['some_event']) {
    // already registered

Alternatively, if your goal is just to prevent registering a callback multiple times, you could precede your on call with an off call:'some_event').on('some_event', () => {
    console.log('it happened.');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download