API集成AngularJS最好的方法

API集成AngularJS最好的方法

本文介绍了什么是与非的RESTful API集成AngularJS最好的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个REST API里面有这样的事情:

I have a REST API which has something like this:

GET /区 - 列出所有区域

GET /zones - lists all zones

{
  "zones": [
    {
      "name": "zone 1",
      "persons": [
        0,
        2,
        3
      ],
      "counter" : 3
    },
    {
      "name": "zone 2",
      "persons": [
        1,
        5
      ],
      "counter" : 0
    }
  ]
}

POST /区 - 创建一个新的区域

POST /zones - creates a new zone

{
  "name": "zone 1",
  "persons": [
    0,
    2,
    3
  ]
}

删除/区域/:ID

删除一个区域

PUT /区域/:ID

更新的区域

现在,终于我有这样的:

and now, finally I have this:

GET /区域/ increment_counter /:ID

其中递增区的计数器参数

which increments the counter parameter of the zone.

我使用的角度和我定义一个工厂为园区对象,它应该从这个REST API养活自己。

I am using Angular and I am defining a factory for the Zone object, which should feed itself from this REST API.

我见过并几乎满足我想要什么,除了增值业务,它不遵循REST风格的指导方针。

I have seen this example and it fulfills almost of what I want, except the increment operation, which does not follow the RESTful guidelines.

我不能修改的REST API,所以我必须处理这个问题。我应该如何处理端点的这些类型的?

I cannot modify the REST API so I have to deal with this. How should I handle these types of endpoints?

此外,我应该用服务,或者可能我只是在我区的工厂定义一个方法(例如:zone.incrementCounter())直接查询服务器,并增加计数器?

Also, should I use services or could I just define a method in my Zone factory (ex.: zone.incrementCounter()) which directly queries the server and increments the counter?

我用Java对象,在这里我只需要定义的干将的和的 setter方法​​的一类的服务器的终结点。

I am used to Java objects, where I just need to define getters and setters for a class and the class will access the server's endpoints under the hood.

什么是最好的方法呢?

推荐答案

第一个问题是,你有什么企图?结果
您是否尝试过?因为这是你应该开始。

First question would be, what have you tried?
Have you tried ngResource? Because that is where you should start.

下面是一个未经检验片段给你它的要点。

Here's an untested snippet to give you the gist of it.

工厂

angular.module('MyApplication')
    .factory('ZoneFactory', ['$resource', function($resource) {
        var url = 'www.example.com/api/zones';
        var paramDefaults = {};
        var methods = {
            'get': {
                'url': url,
                'method': 'GET'
            },
            'post': {
                'url':  url,
                'method': 'POST'
            },
            'delete': {
                'url':  url,
                'method': 'DELETE',
                'params': {
                    'id': '@id'
                }
            },
            'put': {
                'url':  url,
                'method': 'PUT',
                'params': {
                    'id': '@id'
                }
            },
            'increment': {
                'url':  url + '/increment_counter',
                'method': 'GET',
                'params': {
                    'id': '@id'
                }
            }
        };
        return $resource(url, paramDefaults, methods);
    }]);

控制器

angular.module('MyApplication')
    .controller('SomeController', ['ZoneFactory', function(ZoneFactory) {
        var mv = this;

        mv.newZone = {
            'name': '',
            'persons': [],
            'counter': 0
        };
        mv.zones = ZoneFactory.get();

        mv.createZone = function() {
            ZoneFactory.post({'zone': mv.newZone});
            mv.zones.push(mv.newZone);

            mv.newZone = {
                'name': '',
                'persons': [],
                'counter': 0
            };
        };
    }]);

这篇关于什么是与非的RESTful API集成AngularJS最好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 15:58