Skip to content

Components that accept bind-Something can request SomethingExpression#213

Merged
SteveSandersonMS merged 3 commits intomasterfrom
stevesa/component-bind-with-expressions
Feb 18, 2019
Merged

Components that accept bind-Something can request SomethingExpression#213
SteveSandersonMS merged 3 commits intomasterfrom
stevesa/component-bind-with-expressions

Conversation

@SteveSandersonMS
Copy link
Member

This is needed for the forms and validation enhancements in Components for Preview 3. It extends the pattern around binding to component properties with a new optional "*Expression" parameter, e.g.:

    [Parameter] DateTime StartDate { get; set; }
    [Parameter] Action<DateTime> StartDateChanged { get; set; }
    [Parameter] Expression<Func<DateTime>> StartDateExpression { get; set; } // This is new

It's optional, so if the component doesn't declare *Expression, then there's no change to the generated code versus what we had before.

If the component does declare *Expression (where * is something our existing rules match for binding based on the presence of *Changed), then when a consumer uses the binding, e.g.:

    <MyEditor bind-StartDate="@model.StartDate" />

... then it expands to be equivalent to:

    <MyEditor ... StartDateExpression="@(() => model.StartDate)" />

This makes it possible for MyEditor to read any metadata it wants about the thing you're binding to (e.g., validation annotations). What we actually use it for in the new built-in forms/validation system is uniquely identifying the bound field within the EditContext, so for example MyEditor could highlight different states based on whether that field is currently tracked as modified/invalid/etc.

Note: This is only for binding to components. There's no equivalent when binding to plain elements, since plain elements have no way of declaring that they want *Expression (and no reason why they should, since they couldn't do anything with it).

Copy link
Member

@rynowak rynowak left a comment

Choose a reason for hiding this comment

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

This looks really great!

@ajaybhargavb it would be super cool if you would look at this as well - if only to come up to speed on what this does.

@rynowak
Copy link
Member

rynowak commented Feb 15, 2019

@NTaylorMullen FYI in case you are interesting. Dopeness is happening.

Copy link
Contributor

@ajaybhargavb ajaybhargavb left a comment

Choose a reason for hiding this comment

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

Nice!

@SteveSandersonMS SteveSandersonMS merged commit ca9de74 into master Feb 18, 2019
@SteveSandersonMS SteveSandersonMS deleted the stevesa/component-bind-with-expressions branch February 18, 2019 09:37
JunTaoLuo pushed a commit to dotnet/aspnetcore that referenced this pull request May 17, 2020
…dotnet/razor#213)

* In binding to components, automatically supply FooExpression when requested

* Fix tests

* CR feedback
\n\nCommit migrated from dotnet/razor@ca9de74
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants