Reputation: 2569
As often I need an advice to a sencha issue. I want to create a new record / model with a form - and this is working really ok. But on the callback (after saving a record) I need the newly saved record ID - but the ID that exists already in the database - and what I'm getting is an automaticly assigned sencha ID like 'ext-record-465'.
Here goes the code:
MODEL:
Ext.define('aBMin.model.Ticket', {
extend : 'Ext.data.Model',
require : ['aBMin.model.Project', 'aBMin.model.Client'],
config : {
fields : [{
name : 'ticketid',
type : 'int'
}, {
name : 'clientid',
type : 'int'
}, {
name : 'projectid',
type : 'int'
}, {
name : 'subject',
type : 'string'
}, {
name : 'datecreated',
type : 'string'
}, {
name : 'datemodified',
type : 'string'
}, {
name : 'percentCompleted',
type : 'string'
}, {
name : 'ticketdesc',
type : 'string'
}, {
name : 'supportstaffid',
type : 'int'
}, {
name : 'timeestimate',
type : 'int'
}, {
name : 'eta',
type : 'date'
}, {
name : 'warrantysupport_yn',
type : 'string'
}, {
name : 'ourfault_yn',
type : 'string'
}, {
name : 'ticketstatusid',
type : 'int'
}, {
name : 'clientemailid',
type : 'int'
}, {
name : 'clientname',
type : 'string'
}, {
name : 'total',
type : 'int'
}],
idProperty : 'ticketid',
proxy : {
type : 'direct',
reader : {
type : 'json'
},
api : {
read : Ticket.readMobile,
update : Ticket.updateMobile,
create : Ticket.createMobile
}
},
associations : [{
type : 'belongsTo',
model : 'aBMin.model.Project',
primaryKey : 'projectid',
foreignKey : 'projectid',
associationKey : 'Project',
getterName : 'getProject'
}, {
type : 'belongsTo',
model : 'aBMin.model.Client',
primaryKey : 'clientid',
foreignKey : 'clientid',
associationKey : 'Client',
getterName : 'getClient'
}/*,{
type : 'belongsTo',
model : 'aBMin.model.TicketStatus',
primaryKey : 'ticketstatusid',
foreignKey : 'ticketstatusid',
associationKey : 'TicketStatus',
getterName : 'getTicketStatus'
}*/],
validations : [{
type : 'presence',
field : 'projectid'
}, {
type : 'presence',
field : 'subject',
}, {
type : 'presence',
field : 'clientid',
}, {
type : 'presence',
field : 'ticketdesc'
}, {
type : 'presence',
field : 'supportstaffid',
}, {
type : 'presence',
field : 'eta',
}, /*{
type : 'presence',
field : 'timeestimate',
}
,*/ {
type : 'presence',
field : 'ticketstatusid',
}]
}
});
CONTROLLER (Creating method):
case 'Create':
var estimatedtime = parseInt(this.getTicketViewEstymateHours().getValue()) * 60 + parseInt(this.getTicketViewEstymateMinutes().getValue());
record.set(form.getValues());
record.set('timeestimate', estimatedtime);
var validation_errors = record.validate().all;
if(!validation_errors.length) {
record.save({
success : function(record) {
if(record.get('clientemailid') != 0) glob.getEmailViewSubmitCreateTicket().setHidden(true);
Ext.getStore('Ticket').load();
Ext.getStore('TicketSF').load( {
params : {
clientid : record.get('clientid')
,filter : ''
,pagin_from : 0
,pagin_to : 10000
}
} );
getEmailViewTicketField.setValue(record.get('ticketid'));
nav.pop();
Ext.Msg.alert('MESSAGE', 'Ticket has been created.', Ext.emptyFn);
},
failure : function() {
Ext.Msg.alert('MESSAGE', 'Ticket has NOT been crated.', Ext.emptyFn);
});
}
else {
Ext.Msg.alert('MESSAGE', 'You have to fill all required fields', Ext.emptyFn);
for(itm in this.getTicketViewFieldset().getItems().items) {
this.getTicketViewFieldset().getItems().items[itm].removeCls("msg_baddatainputed");
}
for(err in validation_errors) {
for(itm in this.getTicketViewFieldset().getItems().items) {
if(this.getTicketViewFieldset().getItems().items[itm].getName() === validation_errors[err].getField()){
this.getTicketViewFieldset().getItems().items[itm].addCls("msg_baddatainputed");
}
}
}
}
break;
I've tried to use record.load method but to load it I need the ID that I don't have... Any ideas how can I refresh the newly created record and get it's ID form the database?
Upvotes: 0
Views: 892
Reputation: 25041
The server should return the created record's data, the same that is would return for a read operation.
If you support batch operations (i.e. editing multiple records at once), you should also include in each record's data the id generated by Ext, in the property configured in clientIdProperty
.
Upvotes: 2