通过'过滤'角度资源的参数(DreamFactory rest api)

时间:2014-05-05 11:39:18

标签: angularjs rest

我的AngularJS应用程序中的查询参数存在问题

我正在使用DreamFactory rest api从MongoDB中读取文档:

.service('Servant', ['$resource', function($resource) {

        // define and return $resource
        return $resource('https://mydsp.cloud.dreamfactory.com:443/rest/mongodb/tablename',
            {
                // set params to bind too
                app_name: 'myapp',
                fields: '@fields',
                limit: '@limit',
                offset: '@offset',
                filter: '@filter'
            },
            {
                // set update method to 'PUT'
                update: {
                    method: 'PUT'
                }
            }
        )
    }]);

当我设置像"参数=值"的过滤器时,这一切都很有效。但是我没有找到一种方法以JSON格式传递更复杂的过滤器参数,如here所述,使用$ in参数等。是否有人知道正确的语法?

编辑:

尝试了类似

的内容
filter = angular.toJson("{'parameter':{$in:['value1','value2']}}")

没有成功......

2 个答案:

答案 0 :(得分:5)

首先......从您的服务网址中删除该端口。 dreamfactory的'https'指定端口443.您无需明确地执行此操作。第二......你应该能够在你的参数中将SQL样式过滤器作为字符串传递。当您按照自己的方式设置$ resource时,应该能够将params对象传递给它。无需stringify或toJson任何东西。 DreamFactory应该处理它。例如......

这是您的服务:

.service('Servant', ['$resource', function($resource) {


return $resource('https://mydsp.cloud.dreamfactory.com/rest/mongodb/tablename',

            {
                app_name: 'myapp',
                fields: '@fields',
                limit: '@limit',
                offset: '@offset',
                filter: '@filter'
            },
            {

                update: {
                    method: 'PUT'
                }
            }
}]);

使用params对象调用该服务:

// the 'parameter' value in our filter string should relate to a field and/or property

    scope.paramsObj = {

         fields: '*',
         limit: 10,
         offset: 0,
         filter: 'parameter in (5,15)'

    }

// call service and handle promise returned by $resource

    Servant.get(scope.paramsObj).then(
     function(result) {
          // handle success
          // like assign to a var or something
          // here we just log it
          console.log(result)
    },
    function(error) {
         // handle error
         // probably should throw an error here
         // but we just log it here
         console.log(error);

    });

修改

确定。所以......它应该适用于SQL样式的过滤字符串。 DreamFactory已记录一个问题。与此同时,您可以创建自定义$ resource操作来处理过滤器并通过POST隧道传输GET请求。听起来比较容易。请参阅下面的代码。

以下是具有自定义操作的服务

.service('Servant', ['DSP_URL', '$resource', function (DSP_URL, $resource) {


        return $resource(DSP_URL + '/rest/mongohq/Colors', {

            // params to bind to
            app_name: YOUR_APP_NAME_HERE,
            fields: '@fields',
            limit: '@limit',
            offset: '@offset'

        }, {
            // custom $resource action
            'getFiltered': {

                // set our method to post because we have to post
                // our filter object
                method: 'POST',

                // We can transform the data before the post.
                // In the circumstance we do need to stringify
                // So that's what we do here.
                transformRequest: function (data) {
                    return JSON.stringify(data);
                }
            }
        })
    }]);

这是控制器:

.controller('MongoCtrl', ['$scope', 'Servant', function ($scope, Servant) {


        // Create a params object
        // This requests all fields.
        // And we explicitly set the method to
        // GET.  We are tunneling a GET request
        // through our POST because our filter
        // needs to be posted but we really want a GET.
        $scope.params = {
            fields: '*',
            method: 'GET'
        };


        // Call our Service with our custom $resource action
        Servant.getFiltered(

            // Send our params
            $scope.params,

            // Send our filter as post data
            {
                "filter": {
                    "color":  {
                        "$in": ["blue", "white"]
                    }
                }
            },

            // handle success
            function (data) {
                console.log(data)
            },

            // handle error
            function (error) {
                console.log(error)
            })


    }])

答案 1 :(得分:1)

我猜你应该对你的过滤数据进行字符串化:

resource.update( { 
  filter: JSON.stringify( {qty:{$in:[5,15]}} ) 
});

或者以这种方式:

resource.get({id:123}, function() {
  resource.filter = JSON.stringify( {qty:{$in:[5,15]}} );
  resource.$update();
});