Mercurial > lada > lada-client
annotate app/view/widgets/LadaForm.js @ 66:fcb63271d1bd
Created custom Form element which is bound to the model
author | Torsten Irländer <torsten.irlaender@intevation.de> |
---|---|
date | Wed, 05 Jun 2013 15:40:15 +0200 |
parents | |
children | 5c58c0e8936b |
rev | line source |
---|---|
66
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
1 Ext.define('Lada.view.widgets.LadaForm', { |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
2 extend: 'Ext.form.Panel', |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
3 |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
4 alias: 'widget.ladaform', |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
5 /** |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
6 * http://moduscreate.com/expert-ext-js-model-integration-in-forms/ |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
7 */ |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
8 |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
9 /** |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
10 * Can be a reference to a model instance or a model class name. |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
11 */ |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
12 model: null, |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
13 /** |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
14 * Set to the id of the model instance and the model will be loaded for you. |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
15 * Only applicable if model provided is a model class name (string). |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
16 */ |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
17 modelId: null, |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
18 bodyPadding: '10 10', |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
19 border: 0, |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
20 |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
21 initComponent: function() { |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
22 |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
23 this.callParent(); |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
24 |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
25 this.getForm().trackResetOnLoad = true; //Workaround |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
26 |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
27 if (Ext.isString(this.model)) { |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
28 |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
29 //Load a model to be updated |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
30 if (this.modelId) { |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
31 |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
32 Ext.ClassManager.get(this.model).load(this.modelId, { |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
33 failure: this.onModelLoadFailure, |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
34 success: this.onModelLoadSuccess, |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
35 scope: this |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
36 }); |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
37 |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
38 //Load an empty record to be inserted |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
39 } else { |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
40 this.bindModel(Ext.create(this.model, {})); |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
41 } |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
42 |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
43 } else { |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
44 |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
45 //Bind the provided model to be updated |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
46 this.bindModel(this.model); |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
47 |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
48 } |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
49 |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
50 this.addEvents('loadsuccess', 'loadfailure', 'savesuccess', 'savefailure'); |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
51 }, |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
52 |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
53 bindModel: function(model) { |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
54 this.model = model; |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
55 this.loadRecord(model); |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
56 }, |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
57 |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
58 commit: function(callback, scope) { |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
59 if (this.form.isDirty()) { |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
60 this.form.updateRecord(this.model); |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
61 |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
62 this.model.save({ |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
63 callback: function(records, operation) { |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
64 if (operation.wasSuccessful()) { |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
65 console.log('Save was successfull'); |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
66 this.fireEvent('savesuccess', this, records, operation); |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
67 } else { |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
68 console.log('Save was not successfull'); |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
69 var errors = operation.request.scope.reader.jsonData["errors"]; |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
70 this.form.markInvalid(errors); |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
71 this.fireEvent('savefailure', this, records, operation); |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
72 } |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
73 if (callback) { |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
74 callback.call(scope || this, this, operation.wasSuccessful(), this.model); |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
75 } |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
76 }, |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
77 scope: this |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
78 }); |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
79 } |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
80 }, |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
81 |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
82 onModelLoadSuccess: function(record, operation) { |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
83 this.bindModel(record); |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
84 this.fireEvent('loadsuccess', this, record, operation); |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
85 }, |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
86 |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
87 onModelLoadFailure: function(record, operation) { |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
88 this.fireEvent('loadfailure', this, record, operation); |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
89 } |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
90 |
fcb63271d1bd
Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff
changeset
|
91 }); |