Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Remove id dependency #1360

Merged
merged 11 commits into from
Jul 17, 2013
Merged

Remove id dependency #1360

merged 11 commits into from
Jul 17, 2013

Conversation

pfiller
Copy link
Contributor

@pfiller pfiller commented Jul 16, 2013

@kenearley @stof @koenpunt

Some time ago, we made a change in Chosen that made the plugin less dependent on HTML IDs. This had two major benefits: 1) the code became less brittle and 2) it became possible to initialize Chosen outside of a DOM context. Chosen still has some remnants of this ID requirement and I've finally gotten around to cleaning them up in this PR.

Notes about this PR

  • IDs are no longer required on any element within Chosen.
  • The function that generates random IDs for jQuery selects has been ripped out.
  • If the select Chosen is being applied to has an ID already, Chosen still adds an ID to its container as I have found this functionality useful.
  • There is a lot of red in this PR 💥

@@ -152,7 +150,7 @@ class Chosen extends AbstractChosen


test_active_click: (evt) ->
if $(evt.target).parents('#' + @container_id).length
if @container[0] is $(evt.target).parents('.chzn-container')[0]

Choose a reason for hiding this comment

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

Probably not a big deal, but you can also access the element with @container.get(0)

Copy link
Collaborator

Choose a reason for hiding this comment

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

instead of getting all parents matching .chzn-container and getting the first one in it, it would be more efficient to use .closest('.chzn-container') so that it stops traversing the DOM once it finds the first one

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 actually prefer the array syntax to get(0).

c335d5f switches to closest

@@ -152,7 +150,7 @@ class Chosen extends AbstractChosen


test_active_click: (evt) ->
if $(evt.target).parents('#' + @container_id).length
if @container[0] is $(evt.target).closest('.chzn-container')[0]
Copy link
Collaborator

Choose a reason for hiding this comment

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

you don't need the [0]. You can pass the jquery collection to .is(). The collection return by .closest has at most as many elements as the original collection anyway (as it can find 0 or 1 closest matching parent for each element in the collection) so it will only contain 0 or 1 element here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That looks much nicer.

if @container.is($(evt.target).closest('.chzn-container'))

ad2de99

@@ -87,11 +85,10 @@ class AbstractChosen

style = if option.style.cssText != "" then " style=\"#{option.style}\"" else ""

Choose a reason for hiding this comment

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

This line is adding the style attribute with no value when there is no cssText. Could we do this?

style = if option.style.cssText? "" then " style=\"#{option.style}\"" else ""

screen shot 2013-07-17 at 10 18 37 am

Choose a reason for hiding this comment

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

Also, I'd love to get rid of the double quote escaping and just use single quote.

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 don't really like changing unrelated lines. We can open a separate PR

@kenearley
Copy link

This looks good to me. Just one small comment above. :shipit:

pfiller added a commit that referenced this pull request Jul 17, 2013
@pfiller pfiller merged commit 7e59f83 into master Jul 17, 2013
@pfiller pfiller deleted the remove-id-dependency branch July 17, 2013 16:21
pfiller added a commit that referenced this pull request Jul 17, 2013
- Search optgroups #1343
- Change search match style #1344
- Remove memoization of search field width. #1359
- Remove Chosen's ID dependency #1360
- Rebuild Search Results After liszt:updated #1361
- Clear highlight when no results are found #1364
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants