marko-js / marko

A declarative, HTML-based language that makes building web apps fun
https://markojs.com/
MIT License
13.25k stars 643 forks source link

Question: Data Binding. #676

Closed ovidius72 closed 2 years ago

ovidius72 commented 7 years ago

Right now I'm changing the component state with events. When a value changes (e.g. in an input tag) I use the on-change or on-keypress events to handle state changes. However in a form with a lot of states you need to have a lot of event handlers in the component class. It gets unwieldy to have full control over all the business logic. It would be easier and clean to have an attribute like angular or vue.js (v-model, model.. or something like this) that takes a property of the state object and update it automatically. I don't know if such a feature is already available but from the documentation I haven't seen anything like that. Is it possible to implement a 2 way data binding out of the box? or, if not possible, can you suggest the best pattern to manage such cases? Thanks.

gilbert commented 7 years ago

Related: https://github.com/marko-js/marko/issues/528

guilhermeaiolfi commented 6 years ago

I'm evaluating markojs for my next project. I'm between vue.js, svelte.js and marko.js. I love the marko syntax and the way it accepts all javascript types as attributes (not just strings or extra markups). The only thing that I'm questioning right now is why there is no basic data bindings. It's not hard for a form have +50 fields, having one event and a function listener for every field will make my components very polluted with duplicated code.

I've read #528 and it's such a common task for all of us, that it should be built in.

patrick-steele-idem commented 6 years ago

@guilhermeaiolfi Do you have a particular proposal in mind? We are definitely open to ideas and I would agree that dealing with a complex form can get tedious. We are looking for user feedback and if there's a compelling proposal we would be interested in coordinating the effort to get it implemented. I think supporting a :bind modifier for an attribute has some promise since it could be a hint to the compiler to pull in data binding code at runtime. Fortunately, we have the ability to add new features without increasing the runtime weight for all users since we have full control over the Marko compiler.

guilhermeaiolfi commented 6 years ago

@patrick-steele-idem well, svelte use bind:value=''. Since marko and svelte share the same architecture (both compile code instead of being interpreted by a library layer), it may be worth to see how they do it. A quick look shows that they treat those as a special case: https://github.com/sveltejs/svelte/blob/93e51d6ef3bccc5a3948fc5f0dcfdde9d1f413ca/src/generators/dom/visitors/Element/Attribute.js#L53

That looks "dirty" to me. Maybe introducing a general way of binding and creating components for ,