Created
June 20, 2018 17:06
-
-
Save tclain/8201df98ab8708d71534da98dcec5b2c to your computer and use it in GitHub Desktop.
simplest view layer in javascript ever (with jquery)
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
import $ from 'jquery'; | |
const view = ({ | |
template, | |
state, | |
el, | |
} = {}) => { | |
let state = {...state}; | |
const render = () => { | |
const interpolated = template(state); | |
$(el).html(interpolated) | |
} | |
// initial render | |
render(); | |
return { | |
set(slice){ | |
state = Object.assign(state, slice) | |
render(); | |
} | |
} | |
} | |
// usage | |
const v = view({ | |
template : data => `<div>Hello ${data.time}</div>`, | |
state : { | |
hello : "world", | |
time: +new Date() | |
}, | |
el : '#app' | |
}) | |
// change time | |
setInterval(() => { | |
v.set({ | |
time : +new Date() | |
}) | |
}, 1500) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is NOT Optimized and for educational purpose only. Sometimes, we just need to update some values on the screen using jquery. This is an unexpensive, not optimized solution.