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

Fix: changeset.set('prop', undefined); does not update prop #191

Merged
merged 2 commits into from
Jul 29, 2018

Conversation

jelhan
Copy link
Contributor

@jelhan jelhan commented Jun 28, 2017

changeset.set('prop', undefined) does not change value.

Please not that #190 is needed to fix this if skipValidate is true. Since without #190 oldValue is always undefined, condition isEqual(oldValue, value) will always be true if value is undefined and therefore change will not be executed.

@jelhan jelhan changed the title WIP: Fix: setting undefined as new value WIP: Fix: set undefined as new value Jun 28, 2017
@jelhan jelhan changed the title WIP: Fix: set undefined as new value Fix: changeset.set('prop', undefined); does not update prop Jun 28, 2017
addon/index.js Outdated
@@ -494,7 +494,7 @@ export function changeset(obj, validateFn = defaultValidatorFn, validationMap =
this._deleteKey(ERRORS, key);

if (!isEqual(oldValue, value)) {
set(changes, key, value);
changes[key] = value;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite sure why Ember.set fails to update value. I think changing property directly might be okay since we call this.notifyPropertyChange(CHANGES); later.

@BorigTheDwarf
Copy link

+1

@snewcomer
Copy link
Collaborator

snewcomer commented Jul 13, 2018

@jelhan Is this still a problem for ya? We could put in a fix today (sry it's been so long)

@jelhan
Copy link
Contributor Author

jelhan commented Jul 13, 2018

Don't have the time today. I think we should check if this scenario is already tested in master. If not, we could apply the test in this PR. If it fails, we might use the fix provided by this PR. Will try to do it over the weekend.

@jelhan
Copy link
Contributor Author

jelhan commented Jul 14, 2018

Current code base is using provided patch. After a lot of refactoring the code line now lives at: https://github.com/poteto/ember-changeset/blob/master/addon/utils/set-nested-property.js#L41

After setting a property to undefined, changes contains the changed property. So original issue is fixed. But another one is added. change object does not contain a changed property with a value of undefined.

Simple reproduction:

changeset = new Changeset({ prop: 'foo' });
changeset.set('prop', undefined);
changeset.get('changes'); // [{ key: 'prop', value: undefined }]
changeset.get('change'); // {}

This PR now provides additional tests to cover both. Test for change property is failing.

@jelhan
Copy link
Contributor Author

jelhan commented Jul 14, 2018

Reported the issue regarding change upstream: poteto/ember-deep-set#14

@algodave
Copy link

I have an address model with a belongsTo('state') association. I create a changeset from it. In my edit address form the following happens:

  • when user selects a different state, it is correctly set in the changeset
  • when user selects a different country, the state in the changeset should be unset; I tried setting it to either undefined or null, but none of them worked: the old state model stays there in the changeset!

In both cases, I'm using ember-changeset's overridden set method rather than Ember's standard set. I'm using a50fc86 in my project.

@jelhan
Copy link
Contributor Author

jelhan commented Jul 26, 2018

@algodave You could try using this pull request for ember-deep-set. You could do so by selective dependency resolutions feature of yarn. The pull request should fix this issue. Please report your findings here.

@algodave
Copy link

Thanks @jelhan will do!

@snewcomer
Copy link
Collaborator

@jelhan Looks like a minor merge conflict here. ember-deep-set 0.1.4 was published fyi!

@jelhan
Copy link
Contributor Author

jelhan commented Jul 28, 2018

Fixed merge conflicts, which were introduced by my own PR. 😆 Also updated ember-deep-set@0.1.4. Don't get confused by travis passing even without that update. It uses yarn install --no-lockfile.

Copy link
Collaborator

@snewcomer snewcomer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work! Love it 💯

@snewcomer snewcomer merged commit 12b5e36 into poteto:master Jul 29, 2018
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 this pull request may close these issues.

None yet

4 participants