【问题标题】:AngularJS: automatically chaining $resource actionsAngularJS:自动链接 $resource 动作
【发布时间】:2014-04-28 15:26:06
【问题描述】:

我有一个 AngularJS 资源实现,(原则上)可以正常工作。现在特殊情况是:

  • 资源的其中一个字段(属性)是“最后更改的时间戳”
  • 如果它在更新请求中到达服务器,则将被忽略。服务器总是自动设置“最后更改的时间戳”
  • 服务器上的更新方法都以某种方式实现,即响应为空(而不是包含修改后的实体)

所以在这种情况下,要在服务器更新操作后获取“最后更改的时间戳”,我总是必须在更新后立即执行 get。

myEntity.$update( function(){ myEntity.$get(); } );

现在的问题是: AngularJS 是否提供了一种自动链接操作的方法:

  • 在 MyEntity 定义中定义,它需要在 $update 之后始终执行 $get。
  • 然后,在应用程序代码中调用

    myEntitty.$update();
    

【问题讨论】:

标签: javascript angularjs angularjs-resource


【解决方案1】:

感谢@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);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多