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 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 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 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 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; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that unlike Backbone models, we can trigger change events by setting an attribute via
=
https://gist.github.com/theotherzach/8501387#file-user_model-js-L22-L32
https://gist.github.com/theotherzach/8501387#file-input_view-js-L14-L16