Memke
Memke

Reputation: 744

Backbone Collections Custom Events with jQuery

Can Collection instance be binded to a custom event? I tried it with jQuery: $(collectionInstance).on(eventsmap).

When I then try to check registered events:

console.log($.data(collectionInstance, 'events'));

I get all custom events I bound to an instance.

But $(collection).trigger('customevent'); doesn't fire this customevent and event handler is not called.

When I try the same with Backbone.View I can trigger custom events whithout any problems. Why is like that? Am I missing something?

Upvotes: 0

Views: 967

Answers (1)

mu is too short
mu is too short

Reputation: 434606

Backbone collections have Backbone.Events mixed in so collections have on, off, and trigger methods. You want to use those methods rather than trying to wrap the collection in jQuery:

collectionInstance.on('event', callback, context);
// One by one until you're done

and then elsewhere:

collection.trigger('customevent');

Upvotes: 1

Related Questions