Is withParameters still supported in Breeze entity query?

試著忘記壹切 提交于 2019-12-07 11:24:06

问题


I tried to use withParameters method on query like that:

query.withParameters({ includeLocation: true })

Unfortunately my parameter was not added to url. I use breeze.debug.js and I've found this line in it

//queryOptions = __extend(queryOptions, this.parameters);

Is that a bug ? Is withParameters support taken out ? Or do I do something wrong ?

I use oData


回答1:


When .withParameters is used, the parameters are added to the URL by the data service adapter, not by the Breeze core. That's why that line is commented out. This allows the parameters to be encoded differently, depending upon the backend that is used.

That's great, but the data service adapter for OData that ships with Breeze 1.4.8 does not handle .withParameters. The WebApi adapter does, but not the OData adapter. We'll make sure it's added in a future release. In the meantime, you can continue to use your workaround.

This oversight/omission is partly because we don't know any OData services that handle custom parameters. If I may ask, what OData service are you using?




回答2:


It looks like this will hopefully be fixed soon: https://github.com/Breeze/breeze.js/issues/19.

In the meantime, you can use this code as a workaround (kudos to the author of this pull request):

var odataAdapter = breeze.config.getAdapterInstance('uriBuilder', 'OData');
var origBuildUri = odataAdapter.buildUri;
odataAdapter.buildUri = function (entityQuery, metadataStore) {
    var uri = origBuildUri(entityQuery, metadataStore);

    //Add custom query option support to webapi odata.
    //See https://github.com/Breeze/breeze.js/issues/19
    if (entityQuery.parameters !== null && typeof entityQuery.parameters === 'object'
        && Object.keys(entityQuery.parameters).length)
    {
        var queryParams = {};
        for (var param in entityQuery.parameters) {
            if (/^([^\$].*)$/.test(param)) {
                var val = entityQuery.parameters[param];
                if (typeof val == 'string') val = "'" + val + "'";
                queryParams[param] = val;
            }
        }

        //get the uri without the resourceName
        var resourceName = entityQuery.resourceName;
        uri = uri.substr(resourceName.length + 1);

        //build the new uri
        uri = resourceName + toQueryOptionsString(queryParams) + '&' + uri;
    }

    //Copied from breeze.js OData adapter
    function toQueryOptionsString(queryOptions) {
        var qoStrings = [];
        for (var qoName in queryOptions) {
            var qoValue = queryOptions[qoName];
            if (qoValue !== undefined) {
                if (qoValue instanceof Array) {
                    qoValue.forEach(function (qov) {
                        qoStrings.push(qoName + "=" + encodeURIComponent(qov));
                    });
                } else {
                    qoStrings.push(qoName + "=" + encodeURIComponent(qoValue));
                }
            }
        }

        if (qoStrings.length > 0) {
            return "?" + qoStrings.join("&");
        } else {
            return "";
        }
    }

    return uri;
};


来源:https://stackoverflow.com/questions/21853905/is-withparameters-still-supported-in-breeze-entity-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!