Skip to content

Instantly share code, notes, and snippets.

@dostanko
Created June 7, 2013 13:05
Show Gist options
  • Save dostanko/5729104 to your computer and use it in GitHub Desktop.
Save dostanko/5729104 to your computer and use it in GitHub Desktop.
This code allows to intercept ajax requests in Sencha application. It's good base to emulate all the backend for JavaScript client, make it dynamic, add intellect to it, response with delay, and do not touch application code at all. Just include this and relative code.
Ext.require(["Ext.data.JsonP", "Ext.Ajax", "Ext.util.DelayedTask", "Localresponse.response"], function(){
Ext.merge(Ext.Ajax , {
request: function(params){
var serviceId = params.url.match( /\/([^\/\?]+)?\?/ ).pop().split("-").join("_");;
if (undefined !== Localresponse.responses[serviceId]) {
var delay = 500;
if (undefined !== Localresponse.responses[serviceId + "_delay"]) {
delay = Localresponse.responses[serviceId + "_delay"].call();
};
var delayedCallback = Ext.create('Ext.util.DelayedTask', function() {
var response = Localresponse.responses[serviceId].apply(this,[params]);
Ext.callback(params.callback, params.scope || window, [response, true, {responseText: Ext.encode(response)}]);
});
delayedCallback.delay(delay);
} else {
alert("can't process ajax responce " + serviceId);
};
}
});
});
@AnilKumarReddyBelum
Copy link

SCRIPT438: Object doesn't support property or method 'require'
File: XXXXXXXX.jsp, Line: 1073, Column: 1

@AnilKumarReddyBelum
Copy link

In our application we have a lot of jsp's html's and css's, javascript files.
we don't want to inject the above code in all the places,could you please find the solution to use a common place that should applicable to all.

as of now we are using the below code snippet it is working as we expected but code shoukld not keep it in all places such as js especially.

Ext.Ajax.on('beforerequest', function(conn, options) {
var headerVal = 'Csrf_Token';
var token = getUrlParams(headerVal);
alert('Fired : inside before request token--->'+token);
Ext.Ajax.defaultHeaders = {
'Csrf_Token' : token
};
});
});

@AnilKumarReddyBelum
Copy link

AnilKumarReddyBelum commented Dec 20, 2017

could u pls suggest me a common place ... ? in my application

@ErikaBarre
Copy link

in app.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment