Created
April 3, 2019 17:09
-
-
Save gs-ysingh/bc9b43a0948d649a13638d9812c714bc to your computer and use it in GitHub Desktop.
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="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