Skip to content

Instantly share code, notes, and snippets.

@toddzebert
Last active November 18, 2016 08:54
Show Gist options
  • Save toddzebert/4914f17b5f632a24a644904ff550ac08 to your computer and use it in GitHub Desktop.
Save toddzebert/4914f17b5f632a24a644904ff550ac08 to your computer and use it in GitHub Desktop.
The Event module of a simple MVC implementation, in ES6 and module pattern
/**
* Event Listeners and notifications module
*/
var simpleMVC = (function simpleMVC(simple) {
'use strict';
// sender is the context of the Model or View which originates the event
simple._Event = function SimpleEvent(sender) {
this._sender = sender;
this._listeners = [];
};
simple._Event.prototype = {
// add listener closures to the list
attach(listener) {
this._listeners.push(listener);
},
// loop through, calling attached listeners
notify(args) { this._listeners.forEach(
(v, i) => this._listeners[i](this._sender, args)
)
},
};
return simple;
})(simpleMVC || {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment