Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix(select): manage select controller options correctly #9421

Closed
wants to merge 1 commit into from
Closed

fix(select): manage select controller options correctly #9421

wants to merge 1 commit into from

Conversation

shahata
Copy link
Contributor

@shahata shahata commented Oct 3, 2014

Closes #9418

@shahata shahata changed the title fix(select): manage select controller options correctly WIP fix(select): manage select controller options correctly Oct 3, 2014
@shahata
Copy link
Contributor Author

shahata commented Oct 3, 2014

This is still work in progress and requires some refactoring, but I'm putting it out just to see if ppl think that this is the right way to handle this... /cc @btford

@shahata shahata changed the title WIP fix(select): manage select controller options correctly fix(select): manage select controller options correctly Oct 4, 2014
@shahata shahata added this to the 1.3.0-rc.5 milestone Oct 4, 2014
@@ -179,7 +179,7 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
// Workaround for https://code.google.com/p/chromium/issues/detail?id=381459
// Adding an <option selected="selected"> element to a <select required="required"> should
// automatically select the new element
if (element[0].hasAttribute('selected')) {
if (element && element[0].hasAttribute('selected')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

is this change related?

Copy link
Contributor

Choose a reason for hiding this comment

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

ah, nvm, i get it.

@btford
Copy link
Contributor

btford commented Oct 6, 2014

This seems like the right approach.

@shahata
Copy link
Contributor Author

shahata commented Oct 8, 2014

@btford should I merge this then? or do you think it needs more work?

@jeffbcross jeffbcross force-pushed the master branch 2 times, most recently from abdaab7 to 30996f8 Compare October 8, 2014 19:47
@jeffbcross
Copy link
Contributor

Could you add to the commit message to explain what was broken and how this fixes it?

This will need a rebase since @tbosch and I have refactored select. I rebased pretty easily and pushed to my fork: https://github.com/jeffbcross/angular.js/tree/pr-9421

@btford I will assign this and take a deeper look since I've been working in select this week.

@jeffbcross jeffbcross self-assigned this Oct 8, 2014
@jeffbcross
Copy link
Contributor

This wasn't introduced by this commit, but the property names of objects in the test don't match their position in the comprehension expression. e.g.:

scope.robots = [
  {key: 1, value: 'c3p0'},
  {key: 2, value: 'r2d2'}
];

compile('<select ng-model="robot" ' +
          'ng-options="item.key as item.value for item in robots">' +
        '</select>');

should be

scope.robots = [
  {value: 1, label: 'c3p0'},
  {value: 2, label: 'r2d2'}
];

compile('<select ng-model="robot" ' +
          'ng-options="item.value as item.label for item in robots">' +
        '</select>');

This fixes a regression that was introduced in 2bcd02d. Basically, the problem was that render() removed the wrong option from the select controller since it assumed that the option that was removed has the same label as the excessive option in existingOptions, but this is only correct if the option was popped from the end of the array. We now remember for each label whether it was added or removed (or removed at some point and then added at a different point) and report to the select controller only about options that were actually removed or added, ignoring any options that just moved.

Closes #9418
@shahata
Copy link
Contributor Author

shahata commented Oct 8, 2014

@jeffbcross I've rebased this and fixed the commit msg and property names in tests

@jeffbcross
Copy link
Contributor

@shahata I think this is good. It took me some looking around to understand how it was working. What do you think about adding this comment above the function?

/**
* A new labelMap is created with each render.
* This function is called for each existing option with added=false,
* and each new option with added=true.
* - Labels that are passed to this method twice,
* (once with added=true and once with added=false) will end up with a value of 0, and
* will cause no change to happen to the corresponding option.
* - Labels that are passed to this method only once with added=false will end up with a
* value of -1 and will eventually be passed to selectCtrl.removeOption()
* - Labels that are passed to this method only once with added=true will end up with a
* value of 1 and will eventually be passed to selectCtrl.addOption()
*/

I've already got the change locally, so you can just let me know if it's good to go.

@btford btford modified the milestones: 1.3.0-rc.5, 1.3.0-rc.6 Oct 8, 2014
@shahata
Copy link
Contributor Author

shahata commented Oct 9, 2014

Sure, comment is fine

@jeffbcross
Copy link
Contributor

Landed as 2435e2b Thanks!

@jeffbcross jeffbcross closed this Oct 9, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrong option removed from select controller
4 participants