将'filter'参数传递给angular资源(DreamFactory rest api)

Gat*_*per 1 rest angularjs

我的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'
                }
            }
        )
    }]);
Run Code Online (Sandbox Code Playgroud)

当我设置像"参数=值"过滤器,但我没能找到所描述的JSON格式通过更复杂的滤波器特性参数的一种方式这一切的伟大工程在这里,使用$参数等有谁知道这个正确的语法?

编辑:

刚试过像

filter = angular.toJson("{'parameter':{$in:['value1','value2']}}")
Run Code Online (Sandbox Code Playgroud)

没有成功......

egr*_*ous 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'
                }
            }
}]);
Run Code Online (Sandbox Code Playgroud)

使用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);

    });
Run Code Online (Sandbox Code Playgroud)

编辑

好.所以......它应该适用于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);
                }
            }
        })
    }]);
Run Code Online (Sandbox Code Playgroud)

这是控制器:

.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)
            })


    }])
Run Code Online (Sandbox Code Playgroud)