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

fix(select): use $viewValue instead of $modelValue #8937

Closed
wants to merge 1 commit into from
Closed

fix(select): use $viewValue instead of $modelValue #8937

wants to merge 1 commit into from

Conversation

shahata
Copy link
Contributor

@shahata shahata commented Sep 4, 2014

Closes #8929

@caitp
Copy link
Contributor

caitp commented Sep 4, 2014

isn't it technically possible for two options to have the same viewValue? --- I guess it's probably possible for them to have the same modelValue though too.. hm

@shahata
Copy link
Contributor Author

shahata commented Sep 4, 2014

Yes, but then they will also have the same modelValue, the behavior will be consistent with how it works today. In fact, the way I see it, the code we had before this change effectively assumed that $viewValue and $modelValue are identical. This is why we will see all kinds of strange behaviors if we'll try to mix select with $validators, $parsers, $formatters and now also $asyncValidators.

BTW, do you think I should add some tests to show that those things work now?

@caitp
Copy link
Contributor

caitp commented Sep 4, 2014

but then they will also have the same modelValue, the behavior will be consistent with how it works today.

How would they have the same model value the same way they do today? Don't we use their value binding from ng-options, or otherwise ng-value for this instead?

I mean, if you have two optgroups with like, indexed matrix values for values, but two rows have the same view values, that kinda breaks the optgroups here (but not before), does it not?

@shahata
Copy link
Contributor Author

shahata commented Sep 4, 2014

I am not very familiar with this directive, in fact today is the first time I looked at select source code. But assuming I'm not missing anything huge, modelValue and viewValue in this context is the value of the selected element(s). Sorry, I think I don't understand your comment :(

@shahata
Copy link
Contributor Author

shahata commented Sep 5, 2014

@caitp do you mind sending a plunkr example of the optgroup case you mentioned? I can't see how it might be effected by this change. I'm probably missing something...

@caitp
Copy link
Contributor

caitp commented Sep 5, 2014

I have no idea if it would break this or not, lets verify that it doesnt

@shahata
Copy link
Contributor Author

shahata commented Sep 5, 2014

You mean something like this? http://plnkr.co/edit/E2Zg4ueMp3FbIJuLTTGL?p=preview

@shahata
Copy link
Contributor Author

shahata commented Sep 5, 2014

@petebacondarwin - what do you think about this? does it make any sense?

@petebacondarwin
Copy link
Contributor

@shahata - I think this is probably the right thing to do. The select directive was originally written a long long time ago - before NgModelController even existed and its code looked so complicated people steered clear of refactoring it as long as it appeared to work.

So originally the select directive completely subverted how NgModelController does stuff (i.e. it didn't care about $viewValue, $parsers, $formatters (and now $validators) etc. It just watched the model expression on the scope and sorted itself out.

Without actually running this code, it seems like it should work for me. And in fact as @shahata points out will enable much better integration with $parsers et al. I think adding in such tests would be a very good thing to do.

I say let's get this in.

@shahata
Copy link
Contributor Author

shahata commented Sep 5, 2014

👍

Added some tests to show that select plays nicely with ngModelController now.

@shahata shahata changed the title refactor(select): use $viewValue instead of $modelValue fix(select): use $viewValue instead of $modelValue Sep 5, 2014
@@ -1437,6 +1437,130 @@ describe('select', function() {
expect(scope.value).toBe(false);
});
});

describe('ngModelCtrl', function() {
it('should support $parsers', function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: these test names are not explaining what is actually being tested, please fix

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@caitp
Copy link
Contributor

caitp commented Sep 16, 2014

@shahata there does appear to be a bug when it comes to duplicate options (as demonstrated by your plunker, but also verified by rebuilding with your patch). I guess thats a bug with ngOptions though

@shahata
Copy link
Contributor Author

shahata commented Sep 16, 2014

Yeah, it is unrelated to this issue. I can open a separate PR for it, but I'm not sure we should fix it...

'ng-options': 'item for item in [\'first\', \'second\', \'third\']',
});

scope.form.select.$asyncValidators.fail = function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

fail? just call it asyc in this and previous test

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, the first fail was correct, the others are copy & paste ;). fixed!

@IgorMinar
Copy link
Contributor

otherwise this looks good to me

@shahata
Copy link
Contributor Author

shahata commented Sep 30, 2014

oops, this is broken after rebasing with 2bcd02d but I'm too tired to look into it now, will take a look tomorrow

@jeffbcross
Copy link
Contributor

Hey @shahata any luck with the rebase?

@shahata
Copy link
Contributor Author

shahata commented Oct 3, 2014

@jeffbcross - yes, had a few urgent things at work, but now I'm on it. This commit is breaking because of a bug in 2bcd02d
I'll open a separate issue for the bug and just avoid it in this PR so it doesn't fail.

@shahata
Copy link
Contributor Author

shahata commented Oct 3, 2014

Okay, build passes now. This is the issue I mentioned - #9418

@jeffbcross
Copy link
Contributor

Landed, thanks! f717416

@jeffbcross jeffbcross closed this Oct 3, 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.

Bug when using ngModel, $asyncValidators and SELECT elements.
6 participants