将"filter"参数传递给角度资源(DreamFactory rest api)



我有一个问题,从我的AngularJS应用程序查询参数

我正在阅读MongoDB使用DreamFactory rest api的文档,像这样:

.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'
                }
            }
        )
    }]);

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

编辑:

刚刚尝试了

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

First…从您的服务url中删除端口。'https'为梦工厂指定端口443。你不需要显式地做。第二个……您应该能够在参数中以字符串的形式传递SQL样式过滤器。当你设置你的$资源的方式,你应该能够传递一个参数对象给它。不需要字符串化或toJson任何东西。梦工厂应该处理它。例如…

这是您的服务:

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

编辑

Ok。所以…它应该与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)
            })

    }])

我想你应该把你的过滤器数据字符串化:

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

或者这样:

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

最新更新