Skip to content

Commit a83f4b9

Browse files
authored
docs: example parameter passing on data-binding with function currying (#133)
1 parent 75250f9 commit a83f4b9

File tree

1 file changed

+62
-94
lines changed

1 file changed

+62
-94
lines changed

content/guide/data-binding.md

Lines changed: 62 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
---
22
title: Data Binding in NativeScript
3+
contributors:
4+
- Ombuweb
5+
- flipperlite
36
---
47

58
_Data Binding_ refers to a connection (_binding_) and data flow between _ViewModel_ (Model) and _User Interface_ (UI).
@@ -22,28 +25,42 @@ There are various ways to manage data flow, often referred to as data bindings:
2225

2326
```ts
2427
export class HelloWorldModel extends Observable {
25-
name = "John Doe"
26-
27-
...
28-
28+
name = 'John Doe'
2929
}
3030
```
3131

32-
- **One-way (to Model) data binding** - Binding which updates the model in relation to some action in the UI. The best example for this is a [tap](/guide/gestures#tap-gesture) event.
32+
- **One-way (to Model) data binding** - Binding which updates the model in relation to some action in the UI. The best example for this is a [tap](/guide/gestures#tap-gesture) event. Static and data bound arguments can be passed using [function currying](https://javascript.info/currying-partials). Here's a [working example](https://stackblitz.com/edit/nativescript-stackblitz-templates-f6wmfx?file=app%2Fmain-view-model.ts).
3333

3434
```xml
3535
<Button text="Submit" tap="{{ onTap }}"/>
36+
37+
<Button text="Button 1" tap="{{ onTap2('Button 1') }}" />
38+
<Button text="Button 2" tap="{{ onTap2('Button 2') }}" />
39+
40+
<Button text="Button 3" tap="{{ onTap3('Button 3', counter) }}" />
3641
```
3742

3843
```ts
3944
export class HelloWorldModel extends Observable {
45+
public counter: number = 42
4046

41-
onTap(args: EventData){
42-
43-
}
47+
onTap(args: EventData) {
48+
//
49+
}
4450

45-
...
51+
onTap2(source: string) {
52+
return function fnOnTap2(args: EventData) {
53+
console.log('onTap2.source =', source)
54+
}
55+
}
4656

57+
onTap3(source: string, num: number) {
58+
return (args: EventData) => {
59+
console.log('onTap3.source =', source)
60+
console.log('onTap3.num =', num)
61+
this.counter--
62+
}
63+
}
4764
}
4865
```
4966

@@ -55,10 +72,7 @@ export class HelloWorldModel extends Observable {
5572

5673
```ts
5774
export class HelloWorldModel extends Observable {
58-
name = "John Doe"
59-
60-
...
61-
75+
name = 'John Doe'
6276
}
6377
```
6478

@@ -92,129 +106,89 @@ NativeScript supports the following polymorphic binding expressions:
92106
To access a value stored in an object property of the bindingContext, use the propert access expression:
93107

94108
```ts
95-
...
96109
user = {
97-
names:{
98-
first: "Sara"
99-
}
110+
names: {
111+
first: 'Sara',
112+
},
100113
}
101-
...
102114
```
103115

104116
```xml
105-
106117
<Label text="{{ user.name.first }}" textWrap="true" />
107-
108118
```
109119

110-
---
111-
112120
### array access
113121

114122
```xml
115-
116123
<Label text="{{ fruits[0] }}" textWrap="true" />
117-
118124
```
119125

120-
---
121-
122126
### logical operators
123127

124128
You can use the not(`!`) operator to reverse the logical state of a binding context property.
125129

126130
```xml
127-
128131
<Label text="{{ !isUserLoggedIn }}" textWrap="true" />
129-
130132
```
131133

132134
Supported operators: `&&`, `||` and `!`.
133135

134-
---
135-
136136
### unary operators
137137

138138
```xml
139-
140139
<Label text="{{ +age }}" textWrap="true" />
141-
142140
```
143141

144142
```ts
145-
146-
...
147-
148-
age = "33"
149-
143+
age = '33'
150144
```
151145

152146
Converts a property value to a `number`. To convert a property to a `number` and negate it, use the `-` operator.
153147

154-
---
155-
156148
### comparison operators
157149

158150
```xml
159-
160151
<Label text="{{ prop1 > prop2 }}" textWrap="true" />
161-
162152
```
163153

164154
Supported operators: `>`,`<`, `<=`, `>=`, `==`, `!=`, `===`, `!==`.
165155

166-
---
167-
168156
### ternary operator
169157

170158
```xml
171-
172159
<Label text="{{ prop1 ? prop2 : prop3 }}" textWrap="true" />
173-
174160
```
175161

176-
---
177-
178162
### grouping parenthesis
179163

180164
```xml
181-
182165
<Label text="{{ prop1*(prop2 + prop3) }}" textWrap="true" />
183-
184166
```
185167

186-
---
187-
188168
### function calls
189169

190170
```xml
191-
192171
<Label text="{{ someMethod(p1,p2,...,pN) }}" textWrap="true" />
193-
194172
```
195173

196-
---
197-
198174
### comparison operators
199175

200176
```xml
201-
202177
<Label text="{{ property1 > property2 }}" textWrap="true" />
203-
204178
```
205179

206180
Other supported operators are: `<`, `<=`, `>=`, `==`, `!=`, `===`, `!==`.
207181

208-
---
209-
210182
:::tip Note
211183
Special characters need to be escaped as follows:
212184

213-
- double quotes(`"`) => `&quot;`
214-
- single quote(`'`) => `&apos;`
215-
- less than(`<`) => `&lt;`
216-
- greater than(`>`) => `&gt;`
217-
- ampersand(`&`) => `&amp;`
185+
- double quotes: `"` &rightarrow; `&quot;`
186+
- single quote: `'` &rightarrow; `&apos;`
187+
- less than: `<` &rightarrow; `&lt;`
188+
- greater than: `>` &rightarrow; `&gt;`
189+
- ampersand: `&` &rightarrow; `&amp;`
190+
191+
:::
218192

219193
## Using data converters
220194

@@ -223,59 +197,53 @@ Often data within the Model is stored in a way that is optimized for best perfor
223197
<!-- TODO: Add SB+Preview -->
224198

225199
```xml
226-
227-
<StackLayout class="p-20 bg">
228-
229-
<Label text="{{ date | dateConverter('dd-mm-yyyy') }}" textWrap="true" />
230-
<Label text="{{ name | toUpperCaseConverter }}" textWrap="true" />
231-
<Label text="{{ title | toTitle }}" textWrap="true" />
232-
233-
</StackLayout>
234-
200+
<StackLayout class="p-20 bg">
201+
<Label text="{{ date | dateConverter('dd-mm-yyyy') }}" textWrap="true" />
202+
<Label text="{{ name | toUpperCaseConverter }}" textWrap="true" />
203+
<Label text="{{ title | toTitle }}" textWrap="true" />
204+
</StackLayout>
235205
```
236206

237207
```ts
238208
export class HelloWorldModel extends Observable {
239-
240-
name = "nandee"
241-
title = "hello world!"
209+
name = 'nandee'
210+
title = 'hello world!'
242211
date = Date.now() // number
243212
dateConverter = this.formatDate()
244213
toUpperCaseConverter = this.toUpperCase()
245214
toTitle = this.convertToTitle()
246215

247-
...
248-
249-
formatDate() {
216+
formatDate() {
250217
return {
251218
toView(value: number, format: string) {
252-
253219
const date = new Date(value)
254-
const day = date.getDate().toString().padStart(2, "0")
255-
const month = (date.getMonth() + 1).toString().padStart(2, "0") // months are zero based in JS.
220+
const day = date.getDate().toString().padStart(2, '0')
221+
const month = (date.getMonth() + 1).toString().padStart(2, '0') // months are zero based in JS.
256222
const year = date.getFullYear().toString()
257223

258-
return format.replace("dd", day)
259-
.replace("mm", month)
260-
.replace("yyyy", year)
261-
}
224+
return format
225+
.replace('dd', day)
226+
.replace('mm', month)
227+
.replace('yyyy', year)
228+
},
262229
}
263230
}
264231

265-
toUpperCase(){
232+
toUpperCase() {
266233
return {
267-
toView(value:string){
234+
toView(value: string) {
268235
return value.toUpperCase()
269-
}
236+
},
270237
}
271238
}
272239

273-
convertToTitle(){
240+
convertToTitle() {
274241
return {
275-
toView(str:string){
276-
return str.replace(/(^|\s)\S/g, function(t) { return t.toUpperCase() });
277-
278-
}
242+
toView(str: string) {
243+
return str.replace(/(^|\s)\S/g, function (t) {
244+
return t.toUpperCase()
245+
})
246+
},
279247
}
280248
}
281249
}

0 commit comments

Comments
 (0)