annotate app/view/widgets/LadaForm.js @ 84:5ad82cff1a21

Only submit form if internal validation is ok.
author Torsten Irländer <torsten.irlaender@intevation.de>
date Tue, 11 Jun 2013 11:18:17 +0200
parents 8525e3525538
children 079f99229eff
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
75
4a82e28dfcc4 Translate error codes and store them as attributes of the form
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 74
diff changeset
21 errors: null,
4a82e28dfcc4 Translate error codes and store them as attributes of the form
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 74
diff changeset
22 warnings: null,
4a82e28dfcc4 Translate error codes and store them as attributes of the form
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 74
diff changeset
23 message: null,
4a82e28dfcc4 Translate error codes and store them as attributes of the form
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 74
diff changeset
24
66
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
25 initComponent: function() {
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 this.callParent();
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 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
30
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
31 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
32
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
33 //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
34 if (this.modelId) {
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
35
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
36 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
37 failure: this.onModelLoadFailure,
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
38 success: this.onModelLoadSuccess,
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
39 scope: this
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
40 });
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 //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
43 } else {
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
44 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
45 }
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
46
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
47 } else {
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 //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
50 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
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
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
54 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
55 },
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 bindModel: function(model) {
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
58 this.model = model;
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
59 this.loadRecord(model);
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
60 },
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 commit: function(callback, scope) {
84
5ad82cff1a21 Only submit form if internal validation is ok.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 83
diff changeset
63 if (this.form.isDirty() && this.form.isValid()) {
66
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
64 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
65
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
66 this.model.save({
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
67 callback: function(records, operation) {
83
8525e3525538 Move parsing the response in a seperate function.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
68 this.parseResponse(operation);
66
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
69 if (operation.wasSuccessful()) {
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
70 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
71 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
72 } else {
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
73 console.log('Save was not successfull');
75
4a82e28dfcc4 Translate error codes and store them as attributes of the form
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 74
diff changeset
74 this.form.markInvalid(this.errors);
66
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
75 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
76 }
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
77 if (callback) {
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
78 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
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 scope: this
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
82 });
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
83 }
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
84 },
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 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
87 this.bindModel(record);
83
8525e3525538 Move parsing the response in a seperate function.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
88 this.parseResponse(operation);
66
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
89 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
90 },
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
91
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
92 onModelLoadFailure: function(record, operation) {
83
8525e3525538 Move parsing the response in a seperate function.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
93 this.parseResponse(operation);
66
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
94 this.fireEvent('loadfailure', this, record, operation);
74
5c58c0e8936b Add function which translates the errorcodes returned from the server.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 66
diff changeset
95 },
5c58c0e8936b Add function which translates the errorcodes returned from the server.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 66
diff changeset
96
5c58c0e8936b Add function which translates the errorcodes returned from the server.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 66
diff changeset
97 translateReturnCodes: function(codes) {
5c58c0e8936b Add function which translates the errorcodes returned from the server.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 66
diff changeset
98 var translated = {};
5c58c0e8936b Add function which translates the errorcodes returned from the server.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 66
diff changeset
99 for (var k in codes) {
5c58c0e8936b Add function which translates the errorcodes returned from the server.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 66
diff changeset
100 translated[k] = Lada.getApplication().bundle.getMsg(codes[k]);
5c58c0e8936b Add function which translates the errorcodes returned from the server.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 66
diff changeset
101 }
5c58c0e8936b Add function which translates the errorcodes returned from the server.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 66
diff changeset
102 return translated;
83
8525e3525538 Move parsing the response in a seperate function.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
103 },
8525e3525538 Move parsing the response in a seperate function.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
104 parseResponse: function(operation) {
8525e3525538 Move parsing the response in a seperate function.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
105 this.errors = this.translateReturnCodes(operation.request.scope.reader.jsonData["errors"]);
8525e3525538 Move parsing the response in a seperate function.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
106 this.warnings = this.translateReturnCodes(operation.request.scope.reader.jsonData["warnings"]);
8525e3525538 Move parsing the response in a seperate function.
Torsten Irländer <torsten.irlaender@intevation.de>
parents: 75
diff changeset
107 this.message = Lada.getApplication().bundle.getMsg(operation.request.scope.reader.jsonData["message"]);
66
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
108 }
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
109
fcb63271d1bd Created custom Form element which is bound to the model
Torsten Irländer <torsten.irlaender@intevation.de>
parents:
diff changeset
110 });

http://lada.wald.intevation.org