JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://underscorejs.org/underscore-min.js"></script>
<script src="http://backbonejs.org/backbone-min.js"></script>
JavaScript
_.extend(Backbone.Model.prototype, {
on: function (name, callback, context) {
var eventSplitter = /\s+/,
eventBinder = /bind:/;
Backbone.Events.on.call(this, name, callback, context);
if (!eventBinder.test(name) && !eventSplitter.test(name)) {
this.trigger('bind:' + name, name, callback, context);
}
}
});
var Human = Backbone.Model.extend({
initialize: function () {
this.on([
'bind:change',
'bind:one', 'bind:two', 'bind:three',
'bind:change:name',
'bind:multipleFirst', 'bind:multipleSecond'
].join(' '), this.handler);
},
handler: function (name) {
console.log('Someone started listening to "' + name + '" event');
},
emptyCallback: function () {
}
});
var human = new Human({ name: 'Christian Engel' });
human.on('change', human.emptyCallback);
human.on('one two three', human.emptyCallback);
human.on('change:name', human.emptyCallback);
human.on({
'multipleFirst': human.emptyCallback,
'multipleSecond': human.emptyCallback
});