PDA

View Full Version : Submit called many times



martinrame
11 Feb 2012, 6:21 AM
I have a Ext.grid.Panel with a toolbar with Insert/Delete/Edit buttons. It's controller handles click events this way:


onInsertClick: function(button, e, options) { // create a new record
var record = Ext.create('DEMO.model.Customer', {});
record.data.Id = -1;
// create controller passing record as parameter
controller = this.getController('DEMO.controller.CustomerDataForm');
controller.init(record);
controller.onAfterSave = this.onAfterSave;
},

As you can see, the variable "controller" gets an instance of CustomerDataForm controller and passes "record" as parameter. The value "record.data.Id = -1" means it must to an Insert in the database.

DEMO.controller.CustomerDataForm controls an Ext.form.Panel with a Submit button. When clicked, it should submit the form's data to the server and destroy the form.


Ext.define('DEMO.controller.CustomerDataForm', { extend: 'Ext.app.Controller',
stores: [
'MyJsonStore'
],
init: function(record) {
me = this;
view = Ext.widget('frmCustomer');
if(record.data.Id==-1){
view.getForm().url = '/cgi-bin/extdesigner/extjshandler.cgi/CustomerData/insert';
};
view.getForm().loadRecord(record);
view.show();


this.control({
'button[action=submit]': {
click: this.onSubmit
},
'button[action=cancel]': {
click: this.onCancel
},
'frmCustomer' :{
afterclose: this.onAfterClose
}
});
},
onSubmit: function(button) {
console.log('onSubmitClick');
var win = button.up('frmCustomer');
frm = win.getForm();
frm.submit({
success: function(form, action){
console.log('After submit');
win.close();
me.onAfterSave();
},
failure: function(form, action){
switch(action.failureType){
case Ext.form.action.CLIENT_INVALID:
Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
break;
case Ext.form.action.Action.CONNECT_FAILURE:
Ext.Msg.alert('Failure', 'Ajax communication failed');
break;
case Ext.form.action.Action.SERVER_INVALID:
Ext.Msg.alert('Failure', action.result.msg);
break;
}
},
scope: this
});


},
onCancel: function(button) {
view.close();
},
onAfterClose: function(panel, eOpts) {
//view.destroy();
//Ext.Msg.alert('Cancel', 'aaa');
}
});


The problem is that when I click "Insert" tool button for the first time, onSubmit is called once. Then, if I click Insert again, onSubmit is called twice, and so on.

What I'm doing wrong?.
Leonardo.

mitchellsimoens
12 Feb 2012, 7:47 AM
Everytime you call init(), you are adding listeners. You shouldn't call init yourself, your controllers should be loaded up front with the application and it will call init for you.