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

Removing a value from an array is updating both changeset and class property #539

Closed
anandlall112 opened this issue Sep 12, 2020 · 2 comments · Fixed by #555
Closed

Removing a value from an array is updating both changeset and class property #539

anandlall112 opened this issue Sep 12, 2020 · 2 comments · Fixed by #555

Comments

@anandlall112
Copy link

anandlall112 commented Sep 12, 2020

Version

3.9.0

Test Case

import Component from '@glimmer/component';
import {FieldsValidations} from "app/validations/form";
import {action} from '@ember/object';
import Changeset from "ember-changeset";
import lookupValidator from "ember-changeset-validations";

export default class FormComponent extends Component {
  
Example model data structure
    {
      id: 1,
      label: "Reason",
      options: ["test1", "test2", "test3"]
    }

  constructor() {
    super(...arguments);
    this.field = this.args.model;
    this.changeset = new Changeset(this.field, lookupValidator(FieldsValidations), FieldsValidations);
  }


  @action
  deleteOption(option) {
    let options = this.changeset.get('options');
    options.splice(options.indexOf('test2'), 1);
    this.changeset.set('options', [...options]);

    // Both changeset and the class property are updated.
    console.log(this.changeset.get('options')); // Result ["test1", "test3"]
    console.log(this.field.options); // Result ["test1", "test3"]

    //However, updating a string value does not behave like this
    this.changeset.set('label', 'testing');

    console.log(this.changeset.get('label')); // Result 'testing'
    console.log(this.field.label); // Result 'Reason'
  }
}
@anandlall112 anandlall112 changed the title Removing a value from an array is updating both changeset and model data Removing a value from an array is updating both changeset and class property Sep 12, 2020
@anandlall112
Copy link
Author

Any suggestion on how to handle this issue?

snewcomer added a commit that referenced this issue Oct 10, 2020
)

* [Test]: mutating underlying complex objects should not be possible

close #539

* Add form addresses test

ref #551
@snewcomer
Copy link
Collaborator

@anandlall112 I just added a test in #555. Mind taking a look and letting me know how this might differ from your example? Free free to reopen!

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

Successfully merging a pull request may close this issue.

2 participants