Skip to content

refactor(selection-model): expose source model in change event #9288

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/cdk/collections/selection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ describe('SelectionModel', () => {
});

describe('onChange event', () => {
it('should return the model that dispatched the event', () => {
let model = new SelectionModel();
let spy = jasmine.createSpy('SelectionModel change event');

model.onChange!.subscribe(spy);
model.select(1);

let event = spy.calls.mostRecent().args[0];

expect(spy).toHaveBeenCalled();
expect(event.source).toBe(model);
});

it('should return both the added and removed values', () => {
let model = new SelectionModel();
let spy = jasmine.createSpy('SelectionModel change event');
Expand Down
12 changes: 9 additions & 3 deletions src/cdk/collections/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export class SelectionModel<T> {
this._selected = null;

if (this._selectedToEmit.length || this._deselectedToEmit.length) {
const eventData = new SelectionChange(this._selectedToEmit, this._deselectedToEmit);
const eventData = new SelectionChange<T>(this, this._selectedToEmit, this._deselectedToEmit);

if (this.onChange) {
this.onChange.next(eventData);
Expand Down Expand Up @@ -178,11 +178,17 @@ export class SelectionModel<T> {
}

/**
* Describes an event emitted when the value of a MatSelectionModel has changed.
* Event emitted when the value of a MatSelectionModel has changed.
* @docs-private
*/
export class SelectionChange<T> {
constructor(public added?: T[], public removed?: T[]) { }
constructor(
/** Model that dispatched the event. */
public source: SelectionModel<T>,
/** Options that were added to the model. */
public added?: T[],
/** Options that were removed from the model. */
public removed?: T[]) {}
}

/**
Expand Down