Created
February 21, 2016 05:20
-
-
Save rndme/3b1de91629b38c7ebd7d 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
function ViewModel(rootElement){ | |
rootElement = rootElement || document; | |
var model = Object.create(null); | |
function updateElement(elm){ | |
var key = elm.getAttribute("bind"), | |
pre = elm.getAttribute("pre") || "", | |
post = elm.getAttribute("append") || "", | |
prop = elm.getAttribute("prop") || "innerHTML"; | |
requestAnimationFrame(function(){ | |
elm[prop] = unescape(pre) + model[key] + unescape(post); | |
}); | |
} | |
model.set = function(k, v) { | |
if(model[k]===v) return; | |
model[k]=v; | |
[].forEach.call(rootElement.querySelectorAll("[bind='"+k+"']"), updateElement ); | |
return model; | |
}; | |
model.update = function(obj) { | |
Object.keys(obj).forEach(function(k,i,r) { | |
model.set(k, obj[k]); | |
}); | |
return model; | |
}; | |
return model; | |
} // end ViewModel() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment