Skip to content

Commit eb59e6a

Browse files
author
Alexander Popov
committed
Add documentation for the strongly typed MVVM binding.
1 parent a1dadec commit eb59e6a

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

framework/mvvm/bindings/checked.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,33 @@ In this example the second radio button would be checked after calling `kendo.bi
8888
### Important: a group of radio buttons should have the same name attribute
8989

9090
All radio buttons acting as a group should have the same `name` attribute. Only then checking a radio button from the group will uncheck the previously checked one.
91+
92+
93+
## Strongly typed checked binding
94+
Checkbox inputs bound to an array and radio buttons also support strong typing, using the same principles applied to the [strongly typed value binding](value#strongly-typed-value-binding).
95+
96+
### Using the data-type attribute
97+
```html
98+
<div id="view">
99+
<input type="checkbox" name="items" data-bind="checked: items" value="bike"/>
100+
<input type="checkbox" name="items" data-bind="checked: items" value="-1" data-type="number"/>
101+
<input type="checkbox" name="items" data-bind="checked: items" value="true" data-type="boolean"/>
102+
<input type="checkbox" name="items" data-bind="checked: items" value="2015-01-31" data-type="date"/>
103+
104+
<input type="radio" name="group" data-type="date" data-bind="checked: group" value="2015-01-31"/>
105+
<input type="radio" name="group" data-type="datetime-local" data-bind="checked: group" value="2013-06-05T23:13:40"/>
106+
<input type="radio" name="group" data-type="text" data-bind="checked: group" value="Hello"/>
107+
<input type="radio" name="group" data-type="number" data-bind="checked: group" value="3.14"/>
108+
<input type="radio" name="group" data-type="boolean" data-bind="checked: group" value="false"/>
109+
</div>
110+
<script>
111+
var viewModel = kendo.observable({
112+
items: [-1, true],
113+
group: kendo.parseDate("2015-01-31", "yyyy-MM-dd")
114+
});
115+
kendo.bind($("#view"), viewModel);
116+
viewModel.bind("change", function(e){
117+
console.log(e.field, "=", this.get(e.field));
118+
});
119+
</script>
120+
```

framework/mvvm/bindings/value.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,63 @@ The third `option` will be displayed as selected. Selecting another `option` wil
225225

226226
The second `option` will be displayed as selected. Selecting another `option` will append the corresponding item from the `products` array to the
227227
`selectedProducts` array. Unselecting an `option` will remove its corresponding item from the `selectedProducts` array.
228+
229+
## Strongly typed value binding
230+
By default the View-Model fields are updated with string values, as this is what the DOM element's value property contains. Since the 2015 Q1 release, Kendo MVVM allows strongly typed value binding by parsing the element's value before updating the View-Model field bound to it. Supported types are `text`, `number`, `date`, `datetime-local` and `boolean`.
231+
232+
> To be correctly parsed, the `date` and `datetime-local` values should use strict formatting rules, including the leading zeroes:
233+
>
234+
> - `date` - "yyyy-MM-dd"
235+
> - `datetime-local` - "yyyy-MM-ddTHH:mm:ss"
236+
237+
238+
### Using the type attribute
239+
Kendo MVVM automatically uses strongly typed value binding based on the input element's `type` attribute.
240+
```html
241+
<div id="view">
242+
<input type="number" data-bind="value: Quantity"/>
243+
<input type="date" data-bind="value: ArrivalDate"/>
244+
</div>
245+
<script>
246+
var viewModel = kendo.observable({
247+
Quantity: 22,
248+
ArrivalDate : new Date()
249+
});
250+
kendo.bind($("#view"), viewModel);
251+
viewModel.bind("change", function(e){
252+
console.log(e.field, "=", this.get(e.field));
253+
});
254+
</script>
255+
```
256+
257+
### Using the data-type attribute
258+
Explicitly specifying the data type is also supported, via the `data-type` attribute.
259+
```html
260+
<div id="view">
261+
<input type="text" data-type="number" data-bind="value: Quantity"/>
262+
<input type="date" data-type="text" data-bind="value: ArrivalDate"/>
263+
264+
<select multiple="multiple" data-type="number" data-bind="value: number">
265+
<option value="3.14">Pi</option>
266+
<option value="1.41">Pythagoras' constant</option>
267+
<option value="1.61">Golden ratio</option>
268+
</select>
269+
270+
<select data-type="date" data-bind="value: Birthday">
271+
<option value="2015-01-01">John</option>
272+
<option value="2014-12-31">Jane</option>
273+
</select>
274+
</div>
275+
<script>
276+
var viewModel = kendo.observable({
277+
Quantity: 22,
278+
ArrivalDate : kendo.toString(new Date(), "yyyy-MM-dd"),
279+
Birthday: kendo.parseDate("2014-12-31", "yyyy-MM-dd"),
280+
number: [1.61, 3.14]
281+
});
282+
kendo.bind($("#view"), viewModel);
283+
viewModel.bind("change", function(e){
284+
console.log(e.field, "=", this.get(e.field));
285+
});
286+
</script>
287+
```

0 commit comments

Comments
 (0)