In combination with #1699, this proposal seeks to create a more flexible, powerful, and easily understood attribute binding syntaxes for use in stache.
Summary
| Type | Example |
| --- | --- |
| event - viewModel | <my-comp (child-event)="parentMethod()"/>
|
| event - DOM | <my-comp ($click)="parentMethod()"/>
|
| 0 way - viewModel | <my-comp child-prop="foo"/>
|
| one way - viewModel - parent to child | <my-comp {child-prop}="parentProp"/>
|
| one way - DOM - parent to element | <my-comp {$el-prop}="parentProp"/>
|
| one way - reference - child to reference | <my-comp {^child-prop}="*ref"/>
|
| two way - viewModel | <my-comp {(child-prop)}="parentProp"/>
|
| two way - DOM | <my-comp {($child-prop)}="parentProp"/>
|
| two way - reference | <my-comp {(child-prop)}="*ref"/>
|
| two way - reference shorthand | <my-comp *ref/>
|
event - viewModel
(event-name)="StacheMethodExpression"
Binds an event on the can.viewModel
of whatever element it was placed on.
Example:
<modal (todo.created)="hideModal()" (cancelled)="hideModal()"/>
event - DOM
($event-name)="StacheMethodExpression"
Bind an event on the element it was placed on.
Example:
<button ($click)="todo.save()">Save</button>
0 way - viewModel
view-model-attr="value"
Sets the viewModelAttr
property to "value"
.
one way - viewModel - parent to child
The following looks up task
in the scope, and sets it as todo
in the <modal>
's viewModel:
<modal {todo}="task"/>
one way - DOM - parent to element
Provides one-way binding to an element's property or attribute. We will maintain a list of properties that will be set instead of setting the attribute.
<input {$checked}="complete" type='checkbox'/>
This is equivalent to:
<input {{#complete}}checked{{/complete}} type='checkbox'/>
While it's possible to do one-way binding to an element property or attribute with stache alone, two-way binding uses this for syntactic sugar.
one way - reference - child to reference
The following looks up task
in the child viewModel, and sets it as todo
in the current template's
reference scope:
<modal {^task}="*todo"/>
two way - viewModel
This two-way binds a viewModel property to something in the scope. The following two-way binds <component>
's childProp
to parentProp
in the scope:
<component {(child-prop)}="parentProp" />
This is syntactic sugar for:
<component {childProp}="parentProp"
(childProp)="~parentProp(%viewModel.childProp)" />
two way - DOM
This two-way binds a element property to something in the scope. The following two-way binds the <input>
's checked
property to complete
in the scope:
<input {($checked)}="complete" type='checkbox' />
This is syntactic sugar for:
<component {$checked}="complete"
(change)="~complete($element.checked)" />
A mapping of property names to which event should be listened to will be kept.
two way - reference
This two-way binds a element property to something in the scope. The following two-way binds the <component>
's selected.licensePlate
property to selectedPlate
in the template references scope:
<component {(selected.license-plate)}="*selectedPlate"/>
This is syntactic sugar for:
<component {selected.license-plate}="*selectedPlate"
(selected.licensePlate)="~*selectedPlate(%viewModel.selected.licensePlate)" />