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 zone deletion. #30

Merged
merged 1 commit into from
Apr 1, 2015
Merged

Conversation

smarnach
Copy link
Contributor

When deleting a zone, the old version only removed it from the DOM, but not from the underlying data structure that is sent to the server when the save button is pressed. This resulted in zones not actually being deleted. This commit fixes this issue. Also fix a typo in a variable name while we are here.

I have tested this change in a local devstack (which caused more trouble than expected), and it is working fine. There are some bits of the JS code that aren't quite clear to me -- e.g. the difference between _fn.build.form.zone.count and _fn.build.form.zone.formCount. Given that the deadline for this issue is tomorrow, I decided against trying to clean up the code any further than necessary.

@smarnach smarnach force-pushed the smarnach/zone-deletion branch 2 times, most recently from a4d5ddb to e8a469f Compare March 31, 2015 20:32
// The id is of the form "zone-<index>". We extract the index by
// slicing off the first five letters and "casting" to integer.
index = +id.slice(5),
i;
Copy link
Contributor

Choose a reason for hiding this comment

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

This works but seems quite fragile (not your fault, just how the code already was). I'd prefer something like: In the add method, before appending the new DOM node (zoneTemplate(zoneObj)), you can use .data('index', num) to save the index to the node as a JavaScript integer. Then to read the index here, it's just $el.data('index') with no casting or string processing needed.

Eventually, this code should be cleaned up a bit - data is stored redundantly in the DOM and in JavaScript, and there are several ways of identifying each zone (id, index, i, ...). It's quite confusing to read.

Copy link
Contributor

Choose a reason for hiding this comment

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

@smarnach @bradenmacdonald also, is I'm the only one that find declaring a loop variable at the top of the function a strange practice? I.e. instead of for (var i=...) do var i; for (i=...?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bradenmacdonald @e-kolpakov The code will be fragile anyway, and will break if you add a new class to the div. But good point anyway, we can at least make the code we add less fragile, even though it doesn't help right now.
Regarding the loop variable, I intentionally declared it at the top since it is used after the loop as well. I know it would be scoped to the function in any case, but coming from C++, I feel this way gets across the intention better -- it's not limited to the loop. Also, https://github.com/edx/edx-platform/wiki/Javascript-standards-for-the-edx-platform#good-practices (though it doesn't mention loop variables explicitly).

@bradenmacdonald
Copy link
Contributor

There was a build error on Travis CI so I ran the build again for you and it passed. That means it was just a flaky test, and I'm pretty sure it's unrelated to this PR.

I tested this in Studio and it works for me, and the code is no worse than what's there already so 👍

That said, I gave one comment for improvement if time permits. The JS here kinda gives me a headache and I'd love to see it cleaned up and refactored even more but I know there's not time for that now.

id = classes.slice(classes.indexOf('zone-row') + 9),
// The id is of the form "zone-<index>". We extract the index by
// slicing off the first five letters and "casting" to integer.
index = +id.slice(5),
Copy link
Contributor

Choose a reason for hiding this comment

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

@smarnach this will break if any other class added after 'zone-' (e.g. zone-row zone-2 zone-disabled). +1 for @bradenmacdonald 's suggestion below

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@e-kolpakov As noted above, the original code will break in this case anyway. The code needs some rather fundamental refactoring to make it sane. Braden's suggestion is good anyway, and I implemented it.

When deleting a zone, the old version only removed it from the DOM, but not from
the underlying data structure that is sent to the server when the save button is
pressed.  This resulted in zones not actually being deleted.  This commit fixes
this issue.  Also fix a typo in a variable name while we are here.
@smarnach smarnach force-pushed the smarnach/zone-deletion branch from e8a469f to 8869d0c Compare April 1, 2015 13:24
@smarnach
Copy link
Contributor Author

smarnach commented Apr 1, 2015

@bradenmacdonald @e-kolpakov Thanks for the review. I agree the code could do with some refactoring (actually, with quite a bit of it); it is fragile and has a rather questionable choice of data structures. I didn't have time to do this yesterday since I had to learn a lot of things along the way, so I guess this has to wait for some later time.
Regarding data being stored redundantly in the DOM and in JS -- I guess that's necessary to some extent, since the data has to stay around even when the form is gone. The way this redundancy is implemented leaves room for improvement, though.

@e-kolpakov
Copy link
Contributor

@smarnach 👍

smarnach added a commit that referenced this pull request Apr 1, 2015
@smarnach smarnach merged commit ed24dbe into openedx:master Apr 1, 2015
@bradenmacdonald bradenmacdonald deleted the smarnach/zone-deletion branch September 30, 2017 21:18
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.

3 participants