Skip to content

Instantly share code, notes, and snippets.

@gs-ysingh
Created April 3, 2019 17:09
Show Gist options
  • Save gs-ysingh/bc9b43a0948d649a13638d9812c714bc to your computer and use it in GitHub Desktop.
Save gs-ysingh/bc9b43a0948d649a13638d9812c714bc to your computer and use it in GitHub Desktop.
<input id="inputText" type="text" />
function DataBind(domElement, object) {
this.element = domElement.element;
this.attribute = domElement.attribute;
this.event = domElement.event;
this.value = this.element[this.attribute];
var self = this;
this.bindEvents = function() {
var that = self;
this.element.addEventListener(this.event, function(e) {
that.value = e.target[that.attribute];
});
Object.defineProperty(object, object.prop, {
get: function() {
return that.value;
},
set: function(value) {
that.element[that.attribute] = value;
that.value = value;
}
})
}
}
var domElement = {
element: document.getElementById('inputText'),
attribute: 'value',
event: 'keyup'
};
var object = {
prop: 'age'
}
var obj = new DataBind(domElement, object);
obj.bindEvents();
object.age = 23;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment