Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if clone is dirty #111

Open
ericuldall opened this issue Feb 2, 2023 · 3 comments
Open

Check if clone is dirty #111

ericuldall opened this issue Feb 2, 2023 · 3 comments

Comments

@ericuldall
Copy link

One of the more common use cases I have with models is using them in forms. When using a form it's nice to have that baked in with some save button state handling and navigation guards to protect changes.

Is there a flag to see if a model is dirty? I don't see one in the prototype as of now but maybe i'm missing something. The way I see it it could work like this.

<script setup>
// ... assume loaded my model correctly up here
  const model = MyModel({a, b, c});
  const form = model.clone();

  const submit = async () => {
    await form.save();
    form.commit();
  }
</script>
<template @submit.prevent="submit">
  <form>
    <label>A</label><input v-model="form.a" />
    <label>B</label><input v-model="form.b" />
    <label>C</label><input v-model="form.c" />
    <button type="submit" :disabled="!form.__isDirty" />
  </form>
</template>

That's a basic use case. I guess in lieu of something existing on the model we could do model === form ?
Interested to know if there's already a convention for this.

@ericuldall
Copy link
Author

This seems to work okay for now:

const isDirty = computed(() => {
    return JSON.stringify(modelClone) != JSON.stringify(model);
});

Not sure if this is recommended or not.

@marshallswain
Copy link
Owner

That's probably the best way to handle it, really.

@marshallswain
Copy link
Owner

If you haven't checked out Formkit, I'd recommend giving it a shot. It kinda handles the overhead for you. You don't have to
manually bind v-model to elements. It handles reset states, too. Pretty impressive. https://formkit.com/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants