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

Recursive jQuery extend causes undesired result #1100

Open
DanielRLevine opened this issue Apr 26, 2018 · 1 comment · May be fixed by #1102
Open

Recursive jQuery extend causes undesired result #1100

DanielRLevine opened this issue Apr 26, 2018 · 1 comment · May be fixed by #1102

Comments

@DanielRLevine
Copy link

DanielRLevine commented Apr 26, 2018

Expected Behavior
When a field contains an array as its value and is updated, the value returned by editValue should be presented to update_item in the same form.

For example, let's say we have an item that looks like this: {"fruits": ["apple", "banana"]}. Let's say that we edit the item such that the fruits field should only contain ["apple"]. In other words, editValue returns ["apple"]. Then, we would expect update_item to receive the argument {"fruits": ["apple"]}.

Actual Behavior
The value returned by editValue is corrupted before being passed to update_item.

In the example above, editValue returns ["apple"], but update_item receives {"fruits": ["apple", "banana"]}.

Steps to Reproduce
https://jsfiddle.net/jnooq68m/1/

  1. Open console, to watch debug messages.
  2. Click on row to edit it.
  3. Click on checkmark to confirm save.
  4. Even though editValue returns [1], the new value is set to [1, 2, 3].

Steps to Fix
This problem is occurring because of line 1444 in jsgrid.core.js (as of commit f521a20).

updatedItem = $.extend(true, {}, updatingItem, editedItem);

Because of the true in this call, jQuery performs the extend operation recursively. To fix this bug, we should make this extend non-recursive by changing the line to:

updatedItem = $.extend({}, updatingItem, editedItem);

A similar change must also be made on line 1455.

@DanielRLevine
Copy link
Author

It looks, based on the Travis CI checks, that this may be a feature rather than a bug. Is that the case? If so, is there any way to change a field's value from [1, 2] to [1] under the current release of jsGrid?

My intuition is that, by default, the $.extend() should not be recursive, and that if a user really needs their new field value to be extended from the old value, they should save the old value as a property of this in editTemplate and then perform the extend in editValue. This would allow users who want the extend functionality to build what they want, and it would also me to achieve the functionality I was hoping for.

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.

1 participant