Fetchdata.ts
import Vue from "vue";
import axios from 'axios';
import VueAxios from 'vue-axios';
import { GridPlugin, Page, Toolbar, Edit } from "@syncfusion/ej2-vue-grids";
import { DataManager, RemoteSaveAdaptor, UrlAdaptor } from "@syncfusion/ej2-vue-grids/node_modules/@syncfusion/ej2-data";
import Component from "vue-class-component";
Vue.use(GridPlugin);
class custom extends UrlAdaptor {
beforeSend(dm: DataManager, request: XMLHttpRequest): void {
request.setRequestHeader("Authorization", "bearer");
}
}
export default Vue.extend({
data: () => {
return {
toolbar: ['Add', 'Edit','Delete','Update','Cancel'],
editSettings: { allowAdding: true, allowEditing:true, allowDeleting:true },
pageSettings: { pageCount: 3 },
};
},
methods: {
created() {
},
load() {
var localData = new DataManager({
url: "Home/UrlDatasource",
insertUrl: "Home/Insert",
updateUrl: "Home/Update",
removeUrl: "Home/Delete",
adaptor: new custom
});
var grid = (document.getElementsByClassName('e-grid')[0] as any).ej2_instances[0];
grid.dataSource = localData;
}
},
provide: {
grid: [Page, Toolbar,Edit]
}
});
|
export default Vue.extend({
data: () => {
return {
toolbar: ['Add', 'Edit','Delete','Update','Cancel'],
editSettings: { allowAdding: true, allowEditing:true, allowDeleting:true },
pageSettings: { pageCount: 3 },
};
},
methods: {
complete(args) {
debugger;
},
created() {
debugger;
},
load() {
(DataManager.prototype as any).makeRequest = function (url: any, deffered: Deferred, args?: any, query?: any): Object {
. . . . . . . . .
. . . . . . . . .
let req: Object = (this as any).extendRequest(url, fnSuccess, fnFail);
let ajax: Ajax = new Ajax(req);
let getInstance: any = this;
ajax.beforeSend = () => {
(this as any).beforeSend(ajax.httpRequest, ajax);
};
let customajax: Ajax = new Ajax();
customajax.type = 'POST';
(customajax as any).contentType = 'application/json';
customajax.url = '/Home/Data';
customajax.data = JSON.stringify({ gid: [{ OrderID: 1009, CustomerID: "Raja", ShipCity: "India" }] });
customajax.send().then(function (value: any) {
req = ajax.send();
(<Promise<Ajax>>req).catch((e: Error) => true); // to handle failure remote requests.
(getInstance as any).requests.push(ajax);
});
. . . . . . . .
. . . . . . . .
}
var localData = new DataManager({
url: "Home/UrlDatasource",
insertUrl: "Home/Insert",
updateUrl: "Home/Update",
removeUrl: "Home/Delete",
adaptor: new custom
});
var grid = (document.getElementsByClassName('e-grid')[0] as any).ej2_instances[0];
grid.dataSource = localData;
}
},
provide: {
grid: [Page, Toolbar,Edit]
}
});
|
This thread is old but I thought I would add our solution if anyone is interested. We had the same problem and using a custom DataManager in conjunction with a custom DataAdapter worked for us. You can asynchronously update the auth token in executeQuery() and use the result in beforeSend().
I have the same problem and Paul's reply seem to work! Thank you so much for posting it.
Paul, did you have any problem so far with this approach?
Is there any alternative to this that is more 'officially' supported? Is extending data manager considered a good practice?
Thank you!
P.S. I am using Angular.
Hi Michael Kavouklis,
The DataManager functions solely as an synchronous process, therefore, an asynchronous call cannot be utilized in the beforeSend method. However, in your specific scenario, an asynchronous process needs to be incorporated within the operations of DataManager. This customization can be implemented at the sample level to align with user requirements. A demonstration of token authentication has been developed by overriding the makeRequest method. In this approach, an asynchronous call is made to retrieve the token before sending the default request to the server. The token is then stored in the window variable and utilized in the DataManager request. The token can be accessed through the window variable in the beforeSend method. Please review the provided sample and code example for further clarification.
App.component.ts
export class CustomUrlAdaptor extends UrlAdaptor { public beforeSend(dm: any, request: any) { if ((window as any).token) { request.headers.set('Authorization', 'Bearer ' + (window as any).token); // additional headers sent here } super.beforeSend(dm, request); } } async function fetchToken() { const response = await fetch("https://services.odata.org/V4/Northwind/Northwind.svc/Orders"); const movies = await response.json(); return "New Token"; } load() { //load event of Grid (DataManager.prototype as any).makeRequest = function(url: any, deffered: any, args: any, query: any) { const _this = this; let isSelector = !!query.subQuerySelector; let fnFail = function (e:any, req: any) { args.error = e; deffered.reject(args); }; . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . let req = this.extendRequest(url, fnSuccess, fnFail); if (!this.isCustomDataAdaptor(this.adaptor)) { let fetch_1 = new Fetch(req); fetch_1.beforeSend = function () { _this.beforeSend(fetch_1.fetchRequest, fetch_1); }; fetchToken().then(mov => { //once the token is returned from the async method we have stored the token to the window variable token = mov; (window as any).token = token; req = fetch_1.send(); //after we get and set the token to the window variable we have sent the default datamanager request to the server req.catch(function (e: any) { return true; }); // to handle failure remote requests. }); this.requests.push(fetch_1); } . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . return req; } }
|
If you need any other assistance or have additional questions, please feel free to contact us.
Regards
Aishwarya R