Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

feat(chips): trigger ng-change on chip addition/removal #11166

Merged
merged 1 commit into from
Mar 23, 2018

Conversation

chenlijun99
Copy link
Contributor

@chenlijun99 chenlijun99 commented Mar 13, 2018

PR Checklist

Please check that your PR fulfills the following requirements:

  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Infrastructure changes
[ ] Other... Please describe:

What is the current behavior?

ng-change is not supported for md-chips or md-contact-chips.

Issue Number:
#11161 #3857

What is the new behavior?

ng-change is supported on md-chips. It will be triggered on chip addition/removal.

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

Closes #11161 and #3857

@googlebot googlebot added the cla: yes PR author has signed Google's CLA: https://opensource.google.com/docs/cla/ label Mar 13, 2018
@Splaktar Splaktar self-requested a review March 13, 2018 19:10
@Splaktar Splaktar self-assigned this Mar 13, 2018
@Splaktar
Copy link
Member

Sorry for more commit message confusion. Closes #11161 and #3857 needs to be Closes #11161. Closes #3857 for the GitHub parsing to work properly.

@chenlijun99 chenlijun99 force-pushed the fix-issue-11161 branch 2 times, most recently from 72c6242 to 12134e2 Compare March 13, 2018 19:15
Copy link
Member

@Splaktar Splaktar left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution!

  • Please add the appropriate docs (as comments) in the chipsDirective.js and contactChipsDirective.js files for supporting ng-change.
  • Can you please add the use of ng-change to the current Contact Chips demo?
  • Can you please add a demo for ng-change that covers normal Chips?
  • Can you please add a test to the contact-chips.spec.jsfile?

@Splaktar Splaktar added this to the 1.1.9 milestone Mar 13, 2018
@Splaktar Splaktar added in progress Mainly for in progress PRs, but may be used for issues that require multiple PRs P4: minor Minor issues. May not be fixed without community contributions. needs: unit tests This PR needs unit tests to cover the changes being proposed labels Mar 13, 2018
@chenlijun99
Copy link
Contributor Author

@Splaktar Why do we need a test of ng-change for md-contact-chips? Since md-contact-chips is just a wrapper of md-chips, I think it's redudant to test on both the components.

@Splaktar
Copy link
Member

OK, that test may not be needed, as long as we can see it working in the demo. It still seems somewhat useful to have though to make sure that some change to md-contact-chips does not break the ng-change functionality.

@Splaktar Splaktar added needs: review This PR is waiting on review from the team needs: manual testing This issue or PR needs to have some manual testing and verification done and removed needs: unit tests This PR needs unit tests to cover the changes being proposed needs: review This PR is waiting on review from the team labels Mar 14, 2018
Copy link
Member

@Splaktar Splaktar left a comment

Choose a reason for hiding this comment

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

Updated changes LGTM, but I still need to do some manual testing.

Copy link
Contributor

@fsmeier fsmeier left a comment

Choose a reason for hiding this comment

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

I just added two more comments with rly small stuff but to increase clean code in my opinion, but i like the PR overall very much!

@@ -122,6 +124,7 @@ function MdContactChips($mdTheming, $mdUtil) {
contactImage: '@mdContactImage',
contactEmail: '@mdContactEmail',
contacts: '=ngModel',
ngChange: "&",
Copy link
Contributor

Choose a reason for hiding this comment

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

Why double quote here when all the other bindings have 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.

Opss, it just a matter of habit. In the project I'm working on we use double quotes. I'll 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.

In the meantime I've fixed this.

@@ -329,6 +329,7 @@ MdChipsCtrl.prototype.updateChipContents = function(chipIndex, chipContents){
if(chipIndex >= 0 && chipIndex < this.items.length) {
this.items[chipIndex] = chipContents;
this.ngModelCtrl.$setDirty();
this.ngModelCtrl.$setViewValue(this.items.slice());
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this line is repeated 3-times - why not wrap it into a helper-function similar to validateModel() ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems there is already a function called validateModel and I think that updateModel describes better what does this statement do.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But do we really need an helper function for just a single line of instruction?

On the other hand, if you meant to wrap into a function the two lines that are repeated

this.ngModelCtrl.$setDirty();
this.ngModelCtrl.$setViewValue(this.items.slice());

then I think we can remove this.ngModelCtrl.$setDirty(), since AFAIK $setViewValue() should already set the model as $dirty

Copy link
Contributor

Choose a reason for hiding this comment

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

oh, I maybe choosed the wrong words. I meant something like the validateModel(), not name it this way.
But it is 3-times calling a function $setViewValue and chooses the a copy (slice()) from the inner value-cache (this.items). Maybe there is a small change there so the developer has to update now 3-lines instead of one.
This is just an suggestion to wrap this in an extra function because I like to reduce repeated code and make it easier to maintain. But every developer has different workflows.

PS.: if this helper-function could also reduce other duplications it is even better.

@chenlijun99 chenlijun99 force-pushed the fix-issue-11161 branch 2 times, most recently from 36ec784 to 4928ed3 Compare March 14, 2018 14:15
@chenlijun99
Copy link
Contributor Author

@IMM0rtalis
How do you like the changes I made?
In order to make the unit tests pass, I needed to call this.validateModel in updateNgModel as well, which
is kinda redudant since by calling this.ngModelCtrl.$setViewValue the validation is automatically run.
I've added a TODO, since the possible minor refactor would be better done in a different pull-request.

@@ -39,5 +40,9 @@
type: 'unknown'
};
};

self.onModelChange = function(newModel) {
alert("The model has changed");
Copy link
Member

Choose a reason for hiding this comment

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

Can you please use single quotes 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.

Fixed

@@ -84,6 +85,10 @@

}

function onModelChange(model) {
alert("The model has changed");
Copy link
Member

Choose a reason for hiding this comment

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

Can you please use single quotes 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.

Fixed

* Add test of `ng-change` for `md-chips`
* Add docs regarding `ng-change` for `md-chips` and `md-contact-chips`
* Add demo for ng-change on `md-chips`
* Add demo for ng-change on `md-contact-chips`

Closes angular#11161
Closes angular#3857
@Splaktar Splaktar removed the needs: manual testing This issue or PR needs to have some manual testing and verification done label Mar 22, 2018
@Splaktar
Copy link
Member

Testing done. Submitting to presubmit.

@Splaktar Splaktar added pr: merge ready This PR is ready for a caretaker to review and removed in progress Mainly for in progress PRs, but may be used for issues that require multiple PRs labels Mar 22, 2018
@andrewseguin andrewseguin merged commit 19da42d into angular:master Mar 23, 2018
jelbourn added a commit to jelbourn/material that referenced this pull request Apr 6, 2018
@Splaktar
Copy link
Member

Splaktar commented Apr 7, 2018

I'm not sure what the details are yet, but there may be some issue with these changes that may require reverting them. Just a heads up. More details may be available in #11214 at some point.

tinayuangao pushed a commit that referenced this pull request Apr 9, 2018
* Revert "feat(chips): trigger ng-change on chip addition/removal (#11166)"

This reverts commit 19da42d.

* Revert "feat(md-chips): added validation for ng-required (#11125)"

This reverts commit ba0e9fe.
@@ -584,16 +582,21 @@ MdChipsCtrl.prototype.validateModel = function() {
this.ngModelCtrl.$validate(); // rerun any registered validators
};

MdChipsCtrl.prototype.updateNgModel = function() {
this.ngModelCtrl.$setViewValue(this.items.slice());
Copy link
Member

Choose a reason for hiding this comment

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

This line seems to be related to #11304 and #11301.

chmelevskij pushed a commit to chmelevskij/material that referenced this pull request Jun 19, 2018
* Add test of `ng-change` for `md-chips`
* Add docs regarding `ng-change` for `md-chips` and `md-contact-chips`
* Add demo for ng-change on `md-chips`
* Add demo for ng-change on `md-contact-chips`

Closes angular#11161
Closes angular#3857
chmelevskij pushed a commit to chmelevskij/material that referenced this pull request Jun 19, 2018
* Revert "feat(chips): trigger ng-change on chip addition/removal (angular#11166)"

This reverts commit 19da42d.

* Revert "feat(md-chips): added validation for ng-required (angular#11125)"

This reverts commit ba0e9fe.
Splaktar pushed a commit that referenced this pull request Jul 31, 2018
* Add test of `ng-change` for `md-chips`
* Add docs regarding `ng-change` for `md-chips` and `md-contact-chips`
* Add demo for ng-change on `md-chips`
* Add demo for ng-change on `md-contact-chips`

Closes #11161
Closes #3857
Splaktar added a commit that referenced this pull request Jul 31, 2018
* Revert "feat(chips): trigger ng-change on chip addition/removal (#11166)"

This reverts commit 19da42d.

* Revert "feat(md-chips): added validation for ng-required (#11125)"

This reverts commit ba0e9fe.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes PR author has signed Google's CLA: https://opensource.google.com/docs/cla/ P4: minor Minor issues. May not be fixed without community contributions. pr: merge ready This PR is ready for a caretaker to review type: enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat(chips): add support for ng-change
5 participants