Is it possible to cancel Breeze requests (AngularJS)?

巧了我就是萌 提交于 2019-12-24 05:49:54

问题


I'm trying to find a way to cancel BreezeJS queries for the purpose of implementing a live search that queries BreezeJS on keypress (see my other question).

I know that in Angular, $http requests can be cancelled by passing a promise to the timeout parameter in the config object, and then calling resolve() on that promise.

I also know that I can get BreezeJS to use my module's $http insance with:

var ajax = breeze.config.initializeAdapterInstance('ajax', 'angular');
ajax.setHttp($http);

So I was thinking about possibly creating a request interceptor on $http that would set config.timeout to a promise. But that still leaves me quite a few steps away from solving the problem, because I would somehow need to be able to give the promise to my interceptor from the code running the query (so that the querying code could resolve() the promise if needed).

Is there a better way? Is it even possible?


回答1:


Update 16 May 2014

You can set HTTP-level timeout and cancellation with the AJAX Adapter's requestInterceptor as of v.1.4.12. See the documentation, "Controlling AJAX calls".

Original answer

The EntityManager HTTP service operations don't offer cancel or timeout options. Maybe they will someday.

The v.1.4.11 Breeze AJAX adapters don't offer cancel or timeout options either. But they will in v.1.4.12 and you can see the preview on github right now.

The samples on github illustrate both cancel and timeout with the new adapters. For users of the jQuery AJAX component there is DocCode:jQueryAjaxAdapterTests.js. For users of Angular's $http there is the Zza-Node-Mongo:ajax-adapter.async.spec.js.

It's best if you can run the samples but if you can't (perhaps because you don't use one of the technologies involved), the test files (see links above) are easy to read and you should glean the ideas that will help. At least I hope so.

Usage

Cancel and timeout are useful features. Do you need them for your use case? Maybe. But you'd want to take a first step of de-bouncing the text that users enter into your "live search" so that you aren't firing off 100s of pointless queries.

That observation is discussed in your related StackOverflow question.



来源:https://stackoverflow.com/questions/23438398/is-it-possible-to-cancel-breeze-requests-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!