我有一个 AngularJS 资源实现,它(原则上)工作得很好。现在的特殊情况是:
- 资源的字段(属性)之一是“上次更改时间戳”
- 如果它以更新请求的形式到达服务器,则会被忽略。服务器始终自动设置“最后更改的时间戳”
- 服务器上的更新方法都是以响应为空(而不是包含修改的实体)的方式实现的
因此,在这种情况下,要在从服务器执行更新操作后获取“上次更改时间戳”,我总是必须在更新后立即执行获取操作。
myEntity.$update( function(){ myEntity.$get(); } );
现在的问题是:AngularJS 是否提供了一种自动链接操作的方法:
- 在 MyEntity 定义中定义,它需要始终在 $update 之后执行 $get。
然后,在应用程序代码中调用
myEntitty.$update();
最佳答案
感谢@marck 将我推向正确的方向。解决办法基本上是
var MyEntity = $resource( ...,
{
get: {...},
update: {...}
}
);
// params is passed to $update.
// after successful $update, a parameterless $get is called
// success is called after successful $get
// error is called if $update OR $get failed
// returns the promise of the $update
MyEntity.prototype.$updateAndRefresh = function(params, success, error){
var item = this;
return item.$update(params, function(){
item.$get(success, error);
},
error);
}
关于javascript - AngularJS:自动链接 $resource 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23345003/