-
Notifications
You must be signed in to change notification settings - Fork 27.5k
fix(select): avoid checking option element selected properties in render #5994
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -733,6 +733,21 @@ describe('select', function() { | |
expect(sortedHtml(options[2])).toEqual('<option value="1">3</option>'); | ||
}); | ||
|
||
it('should ignore option object selected changes', function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this description should be improved. without reading the test code, it is not clear to me what we are testing here. |
||
createSingleSelect(); | ||
|
||
scope.$apply(function() { | ||
scope.values = [{name: 'A'}, {name: 'B'}, {name: 'C'}]; | ||
scope.selected = scope.values[0]; | ||
}); | ||
|
||
var options = element.find('option'); | ||
var optionToSelect = options.eq(1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to make the test more readable we should also add |
||
optionToSelect.prop('selected', true); | ||
scope.$digest(); | ||
expect(optionToSelect.prop('selected')).toBe(true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and also:
however your change fails this test. can you please look into why and fix it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe that's the correct behavior. The selected property on the option element doesn't always reflect the actual selected value: hovering over an option in FF will set that property, even though the user has not made a selection yet. And since a change event isn't fired when you're just hovering over an option, the model value shouldn't change. |
||
}); | ||
|
||
describe('binding', function() { | ||
|
||
it('should bind to scope value', function() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found that this broke a test on my angular application related to selected option validation. Here's an example of what's broken: http://plnkr.co/edit/hPjcZP?p=preview
If you change the angular version to 1.2.14 or earlier, the behavior I expected works fine. The problem with this fix introduced in 1.2.15, when I revert the value back to the previous selected option each 'existingOption' looks the same as 'option' and so the 'selected' attribute on the select isn't updated. This causes the select element to become out of sync with the model.
Is there a better way to fix this issue? Is there a better way for me to implement my behavior?
I understand that this isn't how validation is typically done, but in my case I would rather revert the model value rather than setting it to undefined, if possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@butchpeters I could be totally wrong, but I would say that what you're doing is not the best use of parsers, since they're meant to transform or inspect values for the model and not to set the values in your view. It looks like 1.3 has some commit and rollback functions which would help here, though.
Is there a reason you can't set the select to invalid ($setValidity)? It's a little weird for me as a user to choose an option and then have it instantly change back (but I don't know what this actually looks like in your app).
The 'correct' behavior here to me would be to set the disabled attribute on the option you don't want chosen. That's hard with ng-options, but there are some solutions here: http://stackoverflow.com/questions/16202254/ng-options-with-disabled-rows. You could also remove the unselectable option with a filter on the ng-options expression.