Skip to content

feat: add a common class to be used when dealing with selection logic - #2562

Merged
tinayuangao merged 7 commits into
angular:masterfrom
crisbeto:selection-model
Jan 13, 2017
Merged

feat: add a common class to be used when dealing with selection logic#2562
tinayuangao merged 7 commits into
angular:masterfrom
crisbeto:selection-model

Conversation

@crisbeto

Copy link
Copy Markdown
Member

Adds the MdSelectionModel class that can be used when dealing with single and multiple selection within a component.

Relates to #2412.

Adds the `MdSelectionModel` class that can be used when dealing with single and multiple selection within a component.
Relates to angular#2412.
@crisbeto
crisbeto requested a review from jelbournJanuary 7, 2017 19:26
@googlebotgooglebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Jan 7, 2017
Comment threadsrc/lib/core/selection/selection.ts Outdated
* Class to be used to power selecting one or more options from a list.
* @docs-private
*/
export class MdSelectionModel {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's omit the Md prefix, since this will be part of the cdk. I'd also be okay with just calling the class Selection (with corresponding SelectionChange) (I do want to avoid prefixing any cdk classes with Cdk)

Comment threadsrc/lib/core/selection/selection.ts Outdated
* Class to be used to power selecting one or more options from a list.
* @docs-private
*/
export class MdSelectionModel {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should be able to make the class generic:

exportclassSelectionModel<T>{ ... }

And then any properties dealing with the item are of type T (or arrays as T[])

Comment threadsrc/lib/core/selection/selection.ts Outdated
private _unflushedDeselectedValues: any[] = [];

/** Keeps track of the selected option that haven't been emitted by the change event. */
private _unflushedSelectedValues: any[] = [];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Move all of these properties above the constructor

Comment threadsrc/lib/core/selection/selection.ts Outdated
private _selectedValues: any[] = [];

/** Keeps track of the deselected options that haven't been emitted by the change event. */
private _unflushedDeselectedValues: any[] = [];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not sure "unflushed" is the right term here. How about something like "pendingDeselection", "deselectedToEmit"?

Comment threadsrc/lib/core/selection/selection.ts Outdated
onChange: Subject<MdSelectionChange> = new Subject();

/** Currently-selected values. */
private _selectedValues: any[] = [];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Rather than keeping an array of selectedValues, we should keep the selection in a Set (named _selection). We can use .keys() when we want the list of selected values (caching the value). This will make selecting, deselecting, and checking selection much faster (and the code simpler). When the options are updated, the update would then look like.

Overall this would change insertion, deletion from O(k) to O(1) and keep getting an array of the selected values mostly the same.

Comment threadsrc/lib/core/selection/selection.ts Outdated
*/
export class MdSelectionModel {
constructor(
private _options: any[],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we can avoid keeping _options in this model completely if we add it as an argument to selectAll. That would make this class much simpler (no update logic, remove _verifyExistence)

@crisbeto

Copy link
Copy Markdown
MemberAuthor

Re-did it based on the feedback @jelbourn.

/**
* Selects a value or an array of values.
*/
select(value: T): void {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it's a bit confusing to have select and _select methods that do different things. Maybe make the private one something like _markSelected? (same for deselect and clear)

@crisbetocrisbetoJan 11, 2017

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

I'd say it's probably better to move the clearing of the selected value to _select instead. That way the only difference is whether the event is emitted.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Done.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I still think, though, that having a select and _select method is something we should avoid. It would be very easy to call the wrong one.

@crisbetocrisbetoJan 11, 2017

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

_markSelected makes sense, but should I rename _deselect and _clear in that case as well?

@crisbetocrisbetoJan 11, 2017

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Changed up the names so they're not easily confused with the public ones.

@jelbourn

Copy link
Copy Markdown
Contributor

LGTM

@jelbournjelbourn added pr: lgtm action: merge The PR is ready for merge by the caretaker and removed pr: needs review labels Jan 11, 2017
@tinayuangao
tinayuangao merged commit c295fa9 into angular:masterJan 13, 2017
@jelbournjelbourn mentioned this pull request Jan 27, 2017
1 task
@angular-automatic-lock-bot

Copy link
Copy Markdown

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-botangular-automatic-lock-botBot locked and limited conversation to collaborators Sep 6, 2019
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: mergeThe PR is ready for merge by the caretakercla: yesPR author has agreed to Google's Contributor License Agreement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@crisbeto@jelbourn@googlebot@tinayuangao