如何在运行时在 ngResource 查询方法中设置超时
how to set timeout in ngResource query method on runtime
我有一个服务保存我所有的 API 端点连接器。这些终结点连接器是 ngResource 对象。
在其中一些中,我覆盖了默认方法,例如get
或query
例如添加responseTransformers
。我希望能够设置get
方法timeout
属性以在发送另一个请求之前停止一个请求,但我想在我的服务中不这样做,我定义了所有$resource
:
this.collections = $resource(BackendConfig.apiUrl + 'collections/:uid/', {}, {
query: {
isArray: false,
method: 'GET',
timeout: myTimeoutPromise
},
});
但是在运行时,就在我执行此操作之前
Resource.collections.query().then(function () {});
通过分析代码,我得出的结论是,可以覆盖参数(第二个参数),但不能覆盖操作(第三个)。
我找到了可能(尚未实现)的解决方案:创建新服务,保留所有超时(假设中断器),在我需要的地方注入我的资源服务,并在那里定义超时承诺。有没有更方便的解决方案?
我不知道
你的$resources做什么,但也许你可以把你的承诺链接到一个提供者中。
$http.get( SOME_URL_HERE ).
then( function(response){
/*Resolve the first promise then chain your other requests*/
var promises = [ ];
promises.push ( $http.get( OTHER_URL ) );
promises.push ( $http.get( AGAIN_OTHER_URL ) );
return $q.all( promises );
})
.then ( function( responses){
/*This 'then' is of the chaining*/
reponses[0].something /*response of the first promise*/
reponses[1].something /*response of the second promise*/
})
.catch( function(error){
console.log( error );
});
相关文章:
- 数组在递归方法中设置为null
- 设置嵌套对象属性的更好方法
- Es6:能够在设置/更新/删除对象属性时调用自定义方法
- sails.js beforeCreate方法只接收required设置为true的模型属性
- 设置显示后Flash对象方法不可用:无
- 是否有任何内置方法可以更改JavaScript对象'的属性设置为某个值
- “new”关键字是在构造对象时自动设置“constructor”属性的唯一方法吗
- 如何重用ajax工厂方法来设置$scoped变量
- 在jquery中,从同一对象的属性设置输入字段和标签的正确方法是什么
- 如何在谷歌地图中设置多个信息窗口,但使用这种特定的JavaScript方法
- 根据需要在控制器中设置方法
- 如何设置方法,该方法只能删除元素的预定义值
- 模拟“类”时,为什么要在 .prototype 属性中设置方法,而不是在构造函数本身中设置方法
- 为什么在 Backbone 的设置方法中有一个 while 循环
- BackboneJS的设置方法不起作用
- OOP 设置方法无法正常工作
- 我如何在Expressjs/nodejs(设置方法)开始调用函数
- 如何处理'undefined'当我在Javascript中为对象设置方法时,从控制台中返回
- 如何在Vue.js中为一个类设置方法
- Javascript Object.defineProperty设置方法触发属性更改