Created
January 23, 2017 08:51
-
-
Save toddzebert/c1ede8b098d4fc256e26613e2cb74d9b to your computer and use it in GitHub Desktop.
The TwoWayView 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
/** | |
* A 2-way View Module | |
*/ | |
var simpleMVC = (function simpleMVC(simple) { | |
'use strict'; | |
// selector is a DOM element that supports .onChanged and .value | |
simple.TwoWayView = function simpleTwoWayView(model, selector) { | |
this._model = model; | |
this._selector = selector; | |
// for 2-way binding | |
this.onChanged = new simple._Event(this); | |
// attach model listeners | |
this._model.onSet.attach( | |
() => this.show() | |
); | |
// attach change listener for two-way binding | |
this._selector.addEventListener("change", | |
e => this.onChanged.notify(e.target.value) | |
); | |
}; | |
simple.TwoWayView.prototype = { | |
show() { | |
this._selector.value = this._model.get(); | |
}, | |
}; | |
return simple; | |
})(simpleMVC || {}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment