Last active
November 18, 2016 08:54
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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