Skip to content
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