PDA

View Full Version : how to stop master ajax request if slave ajax request fails in extjs



manish.sharma1992
18 Aug 2014, 6:08 AM
I developed a small app wherein I have a master and slave concept i.e basis on master's ajax success i have to fire slave's ajax request, if slave fails then master should also fail and vice versa. Please find the below snipnet of my code
Controller.js


Ext.define('Basel3.controller.Contacts', { extend: 'Ext.app.Controller', stores:['...My all store goes here...'], models:['...My all models goes here...'],
views: ['...My all views goes here...'],
refs:[{ My all references goes here }],
init: function(){ this.control({ .... }) },
save: function() { this.getRulesStore().save(); this.getRuleDetailsStore().save(); } }

As in the aforementioned code we can see that I have used the stores to save the data in my database, which fires seperate ajax request, which I don't want to do. Below are the store and model of the respective entities.
Models
Rule Detail:



Ext.define('Basel3.model.RuleDetail', { extend: 'Ext.data.Model', fields: [{name: 'ruleDetailId', type: 'int'}, {name: 'ruleId',type: 'string'}, {name: 'attrName',type: 'string'}, {name: 'attrType',type: 'string'}, {name: 'attrOprtr',type: 'string'}, {name: 'attrValue',type: 'string'} ] });


Rule:

Ext.define('Basel3.model.Rule', { extend: 'Ext.data.Model', fields: [{name: 'ruleName',type: 'string'}, {name: 'ruleGroup', type: 'string'}, {name: 'ruleReportName', type: 'string'}, {name: 'ruleCategory',type: 'string'}, {name: 'ruleTypeId',type: 'string'}, {name: 'ruleFactor',type: 'string'}, {name: 'effDate',type: 'date', dateFormat :'d-M-y'} ] });

Stores:

Rule Details:


Ext.define('Basel3.store.Contacts', { extend: 'Ext.data.Store', model: 'Basel3.model.Contact', autoLoad: true, pageSize: 35, storeId : 'Contacts', autoLoad: {start: 0, limit: 35}, sorters: ['attrType'], groupField: 'attrType', proxy: { type: 'ajax', api: { read : 'ruledetail/view.action', create : 'ruledetail/create.action', update: 'ruledetail/update.action', destroy: '' }, model : 'Basel3.model.Contact', reader: { //reads the data in the JSON Format type: 'json', root: 'data', successProperty: 'success' }, writer: { type: 'json', //writes the data in the JSON Format writeAllFields: true, encode: true, root: 'data' }, listeners: { //Exception Handler for the Ajax Request exception: function(proxy, response, operation){ var error = Ext.decode(response.responseText); Ext.MessageBox.show({ title: 'RULE DETAILS REMOTE EXCEPTION', msg: error.message, icon: Ext.MessageBox.ERROR, buttons: Ext.Msg.OK }); } }} });

Rules:


Ext.define('IntellectB3.store.Rule', { extend: 'Ext.data.Store', model: 'IntellectB3.model.Rule', storeId : 'Rule', autoLoad: true, pageSize: 35, autoSync : true, autoLoad: {start: 0, limit: 35}, proxy: { type: 'ajax', api: { read : 'rule/view.action', create : 'rule/create.action', update: 'rule/update.action', destroy: '' }, reader: { type: 'json', root: 'data', successProperty: 'success' }, writer: { type: 'json', //writes the data in the JSON Format writeAllFields: true, encode: true, root: 'data' }, listeners: { //Exception Handler for the Ajax Request exception: function(proxy, response, operation){ var error = Ext.decode(response.responseText); Ext.MessageBox.show({ title: 'RULE MASTER REMOTE EXCEPTION', msg: error.message, icon: Ext.MessageBox.ERROR, buttons: Ext.Msg.OK }); } }} });

Thanks in advance, waiting for humble replies

chamacs
18 Aug 2014, 4:35 PM
You need to sequentially call the store saves (the API doesn't have a store.save() method, so I'll use sync() as an example). Although the 'vice versa' part is really going to take a custom reversal of the changes done in the first store save (for event when first store successfully saves and second store save fails).

http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.Store-method-sync

so controller function (something like):


save : function() {
this.getRulesStore().sync({
scope : this,
success : function(batch options) {
this.getRulesDetailsStore().sync({
scope : this,
failure : function(batch, options) {
// custom rollback of first store
}
});
}
});
}