Skip to content

[Cookbook/Form] Fixed jQuery bug #2158

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

Merged
merged 4 commits into from
Jan 20, 2013
Merged

[Cookbook/Form] Fixed jQuery bug #2158

merged 4 commits into from
Jan 20, 2013

Conversation

wouterj
Copy link
Member

@wouterj wouterj commented Jan 19, 2013

Q A
Doc fix? yes
New docs? no
Applies to 2.0
Fixed tickets #1382

Description

This PR fixes the old bug while adding new items. If you press the 'add a tag' button 4 times you get the indexes 0, 1, 2, 3. If you remove 1 and 2, you have the indexes 0, 3. If you press the 'add a tag' button twice after this, you expect to get 0, 3, 4, 5, but you got 0, 3, 2, 3.

This PR saves the index in a data-index attribute on the $collectionHolder.

Live examples

Before: http://jsfiddle.net/WouterJ/847Kf/
After: http://jsfiddle.net/WouterJ/847Kf/1/

Side fixes

This PR also improves the jQuery code by adding support of e.preventDefault() for old browsers and by using the data method to get data-* attributes.

At least, this PR fixes a Sphinx syntax bug (.. index: instead of .. index::)

Merge instructions

Because the placeholder changed from $$name$$ to __name__, line 408 should be changed when merging this to 2.1 and master.

$addTagLink.on('click', function(e) {
// prevent the link from creating a "#" on the URL
e.preventDefault();

// add a new tag form (see next code block)
addTagForm(collectionHolder, $newLinkLi);

// `e.preventDefault()` for old browsers
return false;
Copy link
Member

Choose a reason for hiding this comment

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

I don't like this - this stops event propagation, which is a bad practice. Plus, this is a jQuery event, not a normal DOM event, so my impression is that jQuery would make sure that it's compatible with all browsers. Let me know what you think :).

Copy link
Member Author

Choose a reason for hiding this comment

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

@weaverryan thank you for mentioning this. I haven't worked with jQuery for a long time, so I didn't know how jQuery would handle this.

I have done some research and it looks like return false in a jQuery event will cause executing e.preventDefault() and e.stopPropagation() (where e.preventDefault() is executed in a raw JS event).
On the other hand, e.preventDefault() in a jQuery object will execute e.returnValue = false if preventDefault does not exists (which is the method to prevent defaults in old IE browsers).

I'm going to remove this as it does things we don't want it to do. But we should keep this in mind, jQuery 2.0 will not support old IE browsers, which means that the e.preventDefault() method will not execute e.returnValue = false if preventDefault does not exists, which means that we need to do it ourselves.

@weaverryan
Copy link
Member

@wouterj Really nice work looking into this - I just have one comment, and then I'll merge and do the correct steps when merging up.

Thanks!

var newForm = prototype.replace(/\$\$name\$\$/g, index);

// increase the index with one for the next item
collectionHolder.attr('index', index + 1);
Copy link
Member

Choose a reason for hiding this comment

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

this should be .data, not .attr

Copy link
Member Author

Choose a reason for hiding this comment

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

@stof good catch!

weaverryan added a commit that referenced this pull request Jan 20, 2013
[Cookbook/Form] Fixed jQuery bug
@weaverryan weaverryan merged commit 6145217 into symfony:2.0 Jan 20, 2013
@weaverryan
Copy link
Member

Thanks again @wouterj! I've merged into 2.1 and changed to __name__.

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