Skip to content

Instantly share code, notes, and snippets.

@tclain
Created June 20, 2018 17:06
Show Gist options
  • Save tclain/8201df98ab8708d71534da98dcec5b2c to your computer and use it in GitHub Desktop.
Save tclain/8201df98ab8708d71534da98dcec5b2c to your computer and use it in GitHub Desktop.
simplest view layer in javascript ever (with jquery)
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)
@tclain
Copy link
Author

tclain commented Jun 20, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment