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

http://lada.wald.intevation.org