ExtJS - Getting ID of record that was just saved
I need to save some data and return the ID that is created in the SQL 2005 database. I need the ID to pass to another object before saving that so I can associat开发者_Python百科e them correctly.
What is the best way to accomplish this with Ext? Is there anything built into the Framework that makes this simple?
Thanks.
function AddPromotionType() {
var currentDate = new Date();
var newTypeJsonObject = {
promotionTypeId: '0',
promotionType: Ext.getCmp('txtPromoType').getValue(),
updatedBy: userid,
updateDate: currentDate
}
// serialize our service object
var newLevelJsonData = Ext.encode(newTypeJsonObject);
// call the web service and pass over our service object
Ext.lib.Ajax.defaultPostHeader = 'application/json';
Ext.Ajax.request({
url: 'Service/AddPromoType',
method: 'POST',
params: newLevelJsonData,
success: function(response, options) {
AddTypeWindow.destroy();
AddTypeWindow.hide();
// refresh dropdown to reflect new data
Ext.getCmp('newPromoType').getStore().reload();
},
// if data fails to save, show message
failure: function(response, options) {
Ext.MessageBox.alert('Error saving new promotion type', response.responseText);
}
});
}
Assuming your server is passing back the updated data with the new id the response param of your success callback should contain it. When using the facilities built into Stores that automate Ajax calls (e.g., Ext Direct, REST API support, etc.) the id gets automatically updated on the appropriate Record for you and you can handle the store's add
event to inspect the Record. However, since you're doing a manual Ajax call it's up to you to inspect your response if you need the id immediately.
精彩评论