]> git.proxmox.com Git - extjs.git/blame - extjs/build/examples/kitchensink/classic/samples/view/binding/TwoWayFormulasModel.js
add extjs 6.0.1 sources
[extjs.git] / extjs / build / examples / kitchensink / classic / samples / view / binding / TwoWayFormulasModel.js
CommitLineData
6527f429
DM
1Ext.define('KitchenSink.view.binding.TwoWayFormulasModel', {\r
2 extend: 'Ext.app.ViewModel',\r
3\r
4 alias: 'viewmodel.binding-two-way-formulas',\r
5\r
6 formulas: {\r
7 age: {\r
8 get: function (get) {\r
9 return this.getAge(get('birthDate'));\r
10 },\r
11\r
12 set: function (age) {\r
13 var date = this.getData().birthDate,\r
14 now = new Date();\r
15\r
16 if (!date) {\r
17 date = Ext.Date.add(now, Ext.Date.YEAR, -age);\r
18 } else {\r
19 date = new Date(now.getFullYear() - age, date.getMonth(), date.getDate());\r
20 if (this.getAge(date) !== age) {\r
21 date = new Date(now.getFullYear() - age - 1, date.getMonth(), date.getDate());\r
22 }\r
23 }\r
24\r
25 this.set('birthDate', date);\r
26 }\r
27 },\r
28\r
29 fullName: {\r
30 get: function (get) {\r
31 var ret = get('firstName') || '',\r
32 last = get('lastName');\r
33\r
34 if (last) {\r
35 ret += ' ' + last;\r
36 }\r
37\r
38 return ret;\r
39 },\r
40\r
41 // By providing the set method "fullName" is two-way bindable.\r
42 set: function (value) {\r
43 var space = value.indexOf(' '),\r
44 split = (space < 0) ? value.length : space;\r
45\r
46 this.set({\r
47 firstName: value.substring(0, split),\r
48 lastName: value.substring(split + 1)\r
49 });\r
50 }\r
51 }\r
52 },\r
53\r
54 getAge: function (date) {\r
55 var now = new Date(),\r
56 age, birth;\r
57\r
58 if (date) {\r
59 age = now.getFullYear() - date.getFullYear();\r
60 now = now.getMonth() * 100 + now.getDate();\r
61 birth = date.getMonth() * 100 + date.getDate();\r
62 if (now < birth) {\r
63 --age;\r
64 }\r
65 }\r
66 return age || 0;\r
67 }\r
68});\r