Is there a way to listen to all events of a namespace. So when I listen to an event like this:
app.vent.on('notification(:id)', function(type){console.lof(type)})
It will listen to all events likes this:
app.vent.trigger('notification:info')
app.vent.trigger('notification:error')
app.vent.trigger('notification:success')
No. Backbone typically fires a general eventName
event, as well as eventName:specifier
event. An example of this is Model.change
, which allows you to listen to all changes, as well as changes to individual fields:
model.on('change', this.onAnyPropertyChanged);
model.on('change:name', this.onNamePropertyChanged);
Following this pattern in your code, you could trigger your events as follows:
app.vent.trigger('notification', 'info');
app.vent.trigger('notification:info');
And listen to the general event:
app.vent.on('notification', function(type){
console.log(type); //-> "info"
});
As mentioned by in this answer it is not possible to listen events with wildcards. But as you can listen to all
this will work:
vent.on('all', function(evenName, options) {
var type = evenName.split(/notification:/)[1];
if (type) {
console.log(type, options);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With