可以将两种自定义方法添加到ngx-restangular资源吗?

时间:2017-08-30 10:17:21

标签: angular restangular

我正在使用多个方法配置ngx-restangular资源,但只有第一种方法被识别为函数。通过以下配置,可以识别feature功能,但不能识别nearest功能。

restangular.withConfig(
  (configurer) => {

    /* Add Auth header to each request. */
    configurer.addFullRequestInterceptor(
      (element, operation, path, url, headers, params) => {
        return {
          headers: Object.assign({}, headers, {Authorization: `Bearer ${creds.getBearerToken()}`})
        };
      }
    );

    /* For a given locationId, provide a feature set (geometry) for the location. */
    configurer.addElementTransformer('location', false,
      (resource) => {
        resource.addRestangularMethod(
          'feature',
          'get',
          'map'
        );
        return resource;
      }
    );

    /* Resource providing Nearest Location instances suitable for edit. */
    configurer.addElementTransformer('location', true,
      (resource) => {
        resource.addRestangularMethod(
          'nearest',
          'get',
          'nearest'
        );
        return resource;
      }
    );

  }
);
return restangular.service('location');

是否可以添加多个Restangular方法?

1 个答案:

答案 0 :(得分:0)

有可能。我通过为每个要添加的方法调用withConfig来实现此目的:

restangular.withConfig(
  (configurer) => {

    /* Add Auth header to each request. */
    configurer.addFullRequestInterceptor(
      (element, operation, path, url, headers, params) => {
        return {
          headers: Object.assign({}, headers, {Authorization: `Bearer ${creds.getBearerToken()}`})
        };
      }
    );

    /* For a given locationId, provide a feature set (geometry) for the location. */
    configurer.addElementTransformer('location', false,
      (resource) => {
        resource.addRestangularMethod(
          'feature',
          'get',
          'map'
        );
        return resource;
      }
    );

  }
);

restangular.withConfig(
  (configurer) => {

    /* Add Auth header to each request. */
    configurer.addFullRequestInterceptor(
      (element, operation, path, url, headers, params) => {
        return {
          headers: Object.assign({}, headers, {Authorization: `Bearer ${creds.getBearerToken()}`})
        };
      }
    );

    /* Resource providing Nearest Location instances suitable for edit. */
    configurer.addElementTransformer('location', true,
      (resource) => {
        resource.addRestangularMethod(
          'nearest',
          'get',
          'nearest'
        );
        return resource;
      }
    );

  }
);
return restangular.service('location');

当作为withConfig的一部分添加时,还需要为每个标题添加auth标头。