Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom AJAX implementation / adapters #311

Open
AlekseyMartynov opened this issue Nov 29, 2018 · 34 comments
Open

Custom AJAX implementation / adapters #311

AlekseyMartynov opened this issue Nov 29, 2018 · 34 comments

Comments

@AlekseyMartynov
Copy link
Contributor

AlekseyMartynov commented Nov 29, 2018

@AlekseyMartynov
Copy link
Contributor Author

Did some research.
This is likely to be done via the DevExtreme's core/utils/ajax by injecting a custom sendRequest function.

Experimental implementation:
https://github.com/DevExpress/DevExtreme.AspNet.Data/tree/experiment/ng-http-client/experiments

@AlekseyMartynov AlekseyMartynov removed their assignment Mar 21, 2019
@pwen090
Copy link

pwen090 commented Jul 10, 2019

@AlekseyMartynov is there any planned data on support proper Angular HttpClient usage? We use Angular http interceptors extensively in our project so this is a major problem in switching from Telerik (which is all native angular code) to DevExtreme. I see you have an experimental branch but wondering when this would be released/fixed? thank you!

@AlekseyMartynov
Copy link
Contributor Author

@pwen090
The task is currently in the devextreme-angular team Backlog. No due date is set. However, the experimental implementation linked above is self-contained and pluggable. If you want to use it in your project I'll be happy to assist.

@Bykiev
Copy link

Bykiev commented Jul 11, 2019

@AlekseyMartynov, and what about AngularJS? Right now we're using a tweaked version of dx.aspnet.data.js where all ajax calls replaced with $http service...

@AlekseyMartynov
Copy link
Contributor Author

@Bykiev currently no plans to support $http

@Bykiev
Copy link

Bykiev commented Jul 12, 2019

@AlekseyMartynov, thx, but you should consider to implement universal mechanism to bypass this issue, it's really annoying to support our own custom implementation, because interceptors are really useful in Angular/AngularJS apps.

@AlekseyMartynov
Copy link
Contributor Author

@Bykiev

you should consider to implement universal mechanism to bypass this issue

If DevExtreme is referenced as a module (via require/define) then you can override the AJAX implementation as I did in my experimental branch:

ajax = require("devextreme/core/utils/ajax");
createNgHttpClientSendRequestFunc = require("./ng-http-client-helper");
ajax.inject({
sendRequest: createNgHttpClientSendRequestFunc(
createHttpClient(require("@angular/common/bundles/common-http.umd")),
require("devextreme/core/utils/deferred").Deferred
)
});

If you use dx.all.js then I think we can expose ajax.inject there too.

@Bykiev
Copy link

Bykiev commented Jul 12, 2019

@AlekseyMartynov, no, we're not using require/define, exposing ajax.inject seems very promising :) Do I understand correctly, currently it is not supported yet?

@pwen090
Copy link

pwen090 commented Jul 17, 2019

@AlekseyMartynov how would we use the experimental code you linked to above in our regular Angular project?

@AlekseyMartynov
Copy link
Contributor Author

AlekseyMartynov commented Jul 18, 2019

@Bykiev

Do I understand correctly, currently it is not supported yet?

Correct. Without require/define, the ajax module is not visible. I discussed this with the team, and they agreed to expose it in a future release. We'll update this thread when it's done.

@pwen090
I'll prepare an example and share it here.

@AlekseyMartynov
Copy link
Contributor Author

@Bykiev
We have just merged the related PR - DevExpress/DevExtreme#8881.
DevExpress.utils.ajax will be available in the next maintenance update.

@AlekseyMartynov
Copy link
Contributor Author

AlekseyMartynov commented Jul 22, 2019

@pwen090

Please check the sample app at https://github.com/DevExpress/DevExtreme.AspNet.Data.NgHttpClientSample.

@Bykiev
Copy link

Bykiev commented Jul 22, 2019

@AlekseyMartynov, thx!

Do you have any sample how to use it with AngularJS?

@AlekseyMartynov
Copy link
Contributor Author

@Bykiev

No, we don't. I can only suggest an outline:

// execute on app start
DevExpress.utils.ajax.inject({
    sendRequest: function(options) {
        var d = $.Defered();
        
        ...
        
        // on success
        d.resolve(responseBody, "success", xhr);
        
        // on error
        d.reject(xhr, "error");
        
        ...
        
        return d.promise();    
    }
});

xhr should be XMLHttpRequest or an object with at least the following members:

  • status: number
  • statusText: string
  • getResponseHeader(name: string): string
  • responseText: string

@Bykiev
Copy link

Bykiev commented Aug 1, 2019

@AlekseyMartynov, do I understand you correctly, after injecting my logic into DevExpress.utils.ajax I can use it this way?

dataSource: DevExpress.data.AspNet.createStore({
            key: "OrderID",
            loadUrl: "api/Orders",
}),

If so, it's not working for me, sendRequest method is never called. My AngularJS implementation:

DevExpress.utils.ajax.inject({
  sendRequest: function (options) {
	var d = $.Deferred();

	var method = (options.method || 'get').toLowerCase();

	$http({
	  method: method,
	  url: options.url + '?' + $httpParamSerializer(options.data)
	})
	  .success(function (res) {
		d.resolve(res);
	  })
	  .error(function (err) {
		d.reject(err);
	  })

	return d.promise();
  }
});

@AlekseyMartynov
Copy link
Contributor Author

@Bykiev Thanks for trying it out. Looks like the jQuery-based dx.aspnet.data script requires additional modifications to support custom AJAX. I'll check it and post an update.

@AlekseyMartynov
Copy link
Contributor Author

@Bykiev Please try a custom version of dx.aspnet.data.js with [email protected] and let me know whether ajax.inject works with these versions.

@Bykiev
Copy link

Bykiev commented Aug 5, 2019

@AlekseyMartynov, this version is working fine :)

@AlekseyMartynov
Copy link
Contributor Author

@Bykiev Cool. These changes are included in 2.4.4. You can now upgrade and use the official build.

@natalie-o-perret
Copy link

Is it supposed to work with Axios?

@natalie-o-perret
Copy link

@AlekseyMartynov ?

@AlekseyMartynov
Copy link
Contributor Author

Hello @ehouarn-perret.
Please clarify your question. Did you encounter an issue while trying to implement an Axios-backed adapter?

@natalie-o-perret
Copy link

I managed to make it work but it's kind clunky.

Will post something here later on.

Bykiev added a commit to Bykiev/DevExtreme.AspNet.Data.NgHttpClientSample that referenced this issue Aug 4, 2020
According to this DevExpress/DevExtreme.AspNet.Data#311 (comment) you should return xhr value to resolve/reject promise
@Bykiev
Copy link

Bykiev commented Aug 4, 2020

@pwen090

Please check the sample app at https://github.com/DevExpress/DevExtreme.AspNet.Data.NgHttpClientSample.

Hi @AlekseyMartynov , this sample seems to be working incorrectly, because xhr value passed to resolved promise is always null. I've created a pull request

AlekseyMartynov pushed a commit to DevExpress/DevExtreme.AspNet.Data.NgHttpClientSample that referenced this issue Aug 4, 2020
According to this DevExpress/DevExtreme.AspNet.Data#311 (comment) you should return xhr value to resolve/reject promise
@pwen090
Copy link

pwen090 commented Aug 7, 2020

Is there any update on when there will just be built in support for Angular instead of having to do the ng-http-client-helper.ts style workarounds?

@Bykiev
Copy link

Bykiev commented Aug 9, 2020

Is there any update on when there will just be built in support for Angular instead of having to do the ng-http-client-helper.ts style workarounds?

+1 it will be cool to include it into angular-devextreme package

@AlekseyMartynov
Copy link
Contributor Author

I wish I could have the built-in support too. Our DevExtreme framework integration team has this task on the list, but its priority is not very high because of the existing workaround. We'll make an announcement when the feature is ready. Thanks for all your help!

@onur-ozguzel
Copy link

onur-ozguzel commented Feb 17, 2021

Hi all;

Can you somehow implemented refresh-token operation with DevExtreme.AspNet.Data in Angular? I tried some ways but couldn' t succeed it. If we get an 401 error with a request created by DevExtreme.AspNet.Data, i cannot do the operations which i' ve already implemented in http.interceptor. (If we got an 401 error, generate a refresh-token request, if everything is ok, send the failed requests again)

@Bykiev
Copy link

Bykiev commented Jun 4, 2021

@AlekseyMartynov, hello!

It seems with the latest version of devextreme such adapter is not being called anymore (issue with injecting). Can you please check it?

@AlekseyMartynov
Copy link
Contributor Author

Hello @Bykiev

DevExtreme has changed the module structure in 21.1.
Please refer to https://devexpress.com/issue=T985868

You'll need to edit the import statements like in this commit.

@Bykiev
Copy link

Bykiev commented Jun 4, 2021

Thank you!

@AppField
Copy link

AppField commented Dec 6, 2021

Hello @AlekseyMartynov,

in the newest devextreme version the Deferred function is marked as deprecated.

The sample app (https://github.com/DevExpress/DevExtreme.AspNet.Data.NgHttpClientSample) still uses the Deferred function and is not updated to [email protected].

Is there already a new way how to use the HttpClient from Angular?

@raphael-hettich
Copy link

Any news on this matter by any chance ?

@ArsProgramma-private
Copy link

ArsProgramma-private commented Feb 9, 2023

In all honesty feedback on this would be really important.
As it looks the workaround doesn't work with ODataStores and using a Promise inside the beforeSend-Callback doesn't work either.
As nowadays it is basically not possible to use any API without sending and refreshing tokens,
a solution is required urgently, using the HttpClient being the prefered one.

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

No branches or pull requests

8 participants