簡體   English   中英

將'filter'參數傳遞給角度資源(DreamFactory rest api)

[英]Passing 'filter' parameter to angular resource (DreamFactory rest api)

我的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格式通過更復雜的濾波器特性參數的一種方式這一切的偉大工程在這里 ,使用$參數等有誰知道這個正確的語法?

編輯:

剛剛嘗試過類似的東西

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

沒有成功...

首先...從您的服務網址中刪除端口。 dreamfactory的“ https”指定端口443。不需要您明確地進行操作。 其次...您應該能夠在參數中將SQL樣式過濾器作為字符串傳遞。 當您以自己的方式設置$ resource時,應該可以將params對象傳遞給它。 無需進行分類或添加任何內容。 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)
            })


    }])

我想您應該對過濾器數據進行分類處理:

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

或以這種方式:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM