Skip to content

Instantly share code, notes, and snippets.

@toddzebert
Created January 23, 2017 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toddzebert/c1ede8b098d4fc256e26613e2cb74d9b to your computer and use it in GitHub Desktop.
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
/**
* 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