【问题标题】:Using tastypie filters with angularjs and restangular使用带有 angularjs 和 restangular 的美味派过滤器
【发布时间】:2015-03-21 23:02:52
【问题描述】:

我正在尝试使用 AngularJS + Restangular 与在 Django 中使用 Tastypie 创建的 API 进行交互。我已经以example code found here 为起点成功地与 API 进行了交互(如下所示)。

yourApp.config(function(RestangularProvider) {
    RestangularProvider.setBaseUrl("/api");
    RestangularProvider.setResponseExtractor(function(response, operation, what, url) {
        var newResponse;
        if (operation === "getList") {
            newResponse = response.objects;
            newResponse.metadata = response.meta;
        } else {
            newResponse = response;
        }
        return newResponse;
    });
    RestangularProvider.setRequestSuffix('/?');
});

我想在我的 API 调用中使用 Tastypie 的过滤机制,但是这些参数是通过查询字符串而不是 URI 发送的。来自Tastypie docs 的示例:http://localhost:8000/api/v1/entry/?user__username=daniel

除了在每次请求之前重新配置 Restangular 的 setRequestSuffix 选项之外,还有什么干净的方法可以使用 Restangular 在查询字符串中应用 Tastypie 样式的过滤器吗?

【问题讨论】:

  • 如果使用 POST 而不是 GET 会怎样?

标签: javascript django angularjs tastypie restangular


【解决方案1】:

来自https://github.com/mgonto/restangular/issues/301#issuecomment-24273429

// GET to /partners?where={agentID: 1}
Restangular.all('partners').getList({where: '{agentID: 1}'});

// GET to /partners/123?where={agentID: 1}
Restangular.one('partners', 123).get({where: '{agentID: 1}'});

似乎 getList() 在这里可以解决问题。

【讨论】:

    猜你喜欢
    • 2012-10-30
    • 2011-10-24
    • 2014-01-23
    • 2011-10-19
    • 2014-04-18
    • 2013-06-01
    • 1970-01-01
    • 2014-03-15
    • 1970-01-01
    相关资源
    最近更新 更多