Last active
January 3, 2016 18:19
-
-
Save theotherzach/8501387 to your computer and use it in GitHub Desktop.
A Pen by Zach.
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
| <input id="zb-input" required value="" placeholder="Enter your name"> | |
| <br /> | |
| <h4>Current Name: <span id="zb-output"></span> | |
| </h4> |
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
| var app = app || {}; | |
| (function(){ | |
| "use strict"; | |
| var inputView = { | |
| template: document.getElementById('zb-input'), | |
| model: app.userModel, | |
| }; | |
| function handleInput(e) { | |
| this.model.name = e.srcElement.value; | |
| } | |
| inputView.template.addEventListener("keyup", handleInput.bind(inputView)); | |
| app.inputView = inputView; | |
| })(); |
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
| var app = app || {}; | |
| (function(){ | |
| "use strict"; | |
| var outputView = { | |
| template: document.getElementById('zb-output'), | |
| model: app.userModel, | |
| render: function() { | |
| this.template.innerText = this.model.name; | |
| }, | |
| }; | |
| outputView.model.onNameChange(outputView.render.bind(outputView)); | |
| app.outputView = outputView; | |
| })(); |
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
| var app = app || {}; | |
| (function(){ | |
| "use strict"; | |
| var _name = "default value"; | |
| var listeners = []; | |
| var userModel = { | |
| onNameChange: function(fun) { | |
| listeners.push(fun); | |
| }, | |
| notifyListeners: function(value, model) { | |
| listeners.forEach(function(listener) { | |
| listener.call(this, value, model); | |
| }); | |
| }, | |
| }; | |
| Object.defineProperties(userModel, { | |
| name: { | |
| get: function() { | |
| return _name; | |
| }, | |
| set: function(name) { | |
| _name = name; | |
| this.notifyListeners(name, userModel); | |
| }, | |
| }, | |
| }); | |
| app.userModel= userModel; | |
| })(); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty