部分更新(又名PATCH)使用$资源基础的服务? [英] Partial Updates (aka PATCH) using a $resource based service?

查看:112
本文介绍了部分更新(又名PATCH)使用$资源基础的服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在构建使用Django / TastyPie作为后端REST服务提供商的Web应用程序,并建立一个基于AngularJS的前端,使用大量基于$资源服务到服务器上的CRUD的对象。一切是伟大的工作为止!

We're building a web application using Django/TastyPie as the back-end REST service provider, and building an AngularJS based front end, using lots of $resource based services to CRUD objects on the server. Everything is working great so far!

但是,我们想减少,当我们要更新只有一个或两个对象上更改领域,我们正在围绕航运的数据量。

But, we would like to reduce the amount of data that we're shipping around when we want to update only one or two changed fields on an object.

TastyPie支持此使用HTTP PATCH方法。我们已经对我们的对象定义为.diff()方法,因此我们可以决定我们想,当我们做一个更新要发送的领域。我只是找不到如何定义/落实$资源返回的实例对象的方法来完成我们想要的任何文件。

TastyPie supports this using the HTTP PATCH method. We have defined a .diff() method on our objects, so we can determine which fields we want to send when we do an update. I just can't find any documentation on how to define/implement the method on the instance object returned by $resource to do what we want.

我们要做的就是添加另一种方法的对象实例(为这里的Angular.js文档中描述)喜欢的为MyObject $ partialupdate()的将:

What we want to do is add another method to the object instances, (as described in the Angular.js documentation here) like myobject.$partialupdate() which would:


  1. 拨打我们的.diff作()函数来确定哪些字段发送,然后

  2. 使用一个HTTP PATCH请求到仅那些字段发送到服务器。

到目前为止,我无法找到任何文件(或其他SO职位),说明如何做到这一点,但真的很AP preciate任何建议,任何人都可能有。

So far, I can't find any documentation (or other SO posts) describing how to do this, but would really appreciate any suggestions that anyone might have.

感谢您。

推荐答案

我们实施 $补丁使用ngResource,但它是一个有点麻烦(我们使用Django的REST框架上服务器端)。为了您的差异组件,当我离开自己的实现。我们用一个纯净的缓存来追踪资源的变化,让我可以轮询给定对象,看看有什么(如果有的话)已经改变了。

We implemented $patchusing ngResource, but it's a bit involved (we use Django Rest Framework on the server-side). For your diff component, I'll leave to your own implementation. We use a pristine cache to track changes of resources, so I can poll a given object and see what (if any) has changed.

我利用下划线的 _。挑()的方法来拉动已知油田保存关闭现有的实例,创建一个副本(与已知的主键一起)和保存在使用 $补丁

I leverage underscore's _.pick() method to pull the known fields to save off the existing instance, create a copy (along with the known primary key) and save that using $patch.

我们也使用一些工具类来扩展内置的资源。

We also use some utility classes to extend the built-in resources.

app.factory 'PartUpdateMixin', ['$q', '_', ($q, _) ->

    PartUpdateMixin = (klass) ->
        partial_update: (keys...) ->
            deferred = $q.defer()
            params = _.pick(@, 'id', keys...)
            o = new klass(params)
            o.$patch(deferred.resolve, deferred.reject)
            return deferred.promise
]

下面是实用程序类,以提高资源。

Here's the utility classes to enhance the Resources.

app.factory 'extend', ->
    extend = (obj, mixins...) ->
        for mixin in mixins
            obj[name] = method for name, method of mixin
        obj

app.factory 'include', ['extend', (extend) ->
    include = (klass, mixins...) ->
        extend klass.prototype, mixins...

    return include
]

最后,我们可以提高我们的资源

Finally, we can enhance our Resource

include TheResource, PartUpdateMixin(TheResource)
resourceInstance = TheResource.get(id: 1234)
# Later...
updatedFields = getChangedFields(resourceInstance)
resourceInstance.partial_update(updatedFields...)

这篇关于部分更新(又名PATCH)使用$资源基础的服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆