question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Syncing model property with DOM value.

See original GitHub issue

@leeoniya

I’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:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
leeoniyacommented, Jul 13, 2017
0reactions
lawrence-dolcommented, Jul 13, 2017

there is no cloning. it just passes the args through

Nevermind… it was me, buried in some library code.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found