Syncing model property with DOM value.
See original GitHub issueI’ve developed this function to keep a model property in sync with the DOM value; it takes a model object, property name and attribute name and sets up the value assignment and an oninput
handler. It also allows the backing model property to be either a simple value or get/set wrapper.
The thing I haven’t catered for is having multiple handlers, which would allow application code can add oninput
handling on top of this.
What do you think of it in the context of DOMVM?
function modelSync(obj,prp,atr) {
return { [atr]: modelValue(obj,prp), oninput: [modelUpdateHandler,obj,prp,atr] };
}
function modelUpdateHandler(obj, prp, atr, evt) {
modelValue(obj,prp,(evt && evt.target && evt.target[atr]));
}
function modelValue(obj,prp,val) {
if(obj[prp]==undefined) { throw new Error("Cannot find property '"+prp+"' in object",obj); } // require defined property to help find coding errors
return val==undefined
? genU.isFunction(obj[prp]) ? obj[prp]() : obj[prp]
: genU.isFunction(obj[prp]) ? obj[prp](val) : obj[prp]=val;
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Keep a DOM input and state value in sync with the `x-model ...
Iterate through data with the `x-for` attribute in Alpine JS ... Keep a DOM input and state value in sync with the `x-model`...
Read more >Attributes and properties - The Modern JavaScript Tutorial
So, DOM properties and methods behave just like those of regular JavaScript objects: They can have any value. They are case-sensitive (write ...
Read more >Understanding Relationships Between HTML Attributes ...
In this tutorial, you will learn the relationship between HTML attributes and DOM object's properties.
Read more >How to sync Knockout.JS with data already rendered in DOM ...
The question restated is how do you sync the model and view if you already have loaded and displayed items from the server?...
Read more >Using Attributes and Properties in Custom Elements
In some cases, properties and attributes are in sync and this is when reflection takes place. This ensures that when the property value...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
there is no cloning. it just passes the args through [1],[2]
[1] https://github.com/leeoniya/domvm/blob/3.x-dev/src/view/patchEvent.js#L78 [2] https://github.com/leeoniya/domvm/blob/3.x-dev/src/view/patchEvent.js#L45
Nevermind… it was me, buried in some library code.