breeze query where子句未执行

时间:2015-01-05 06:00:29

标签: breeze where client-side

我很习惯使用微风,而且目前只是看起来非常简单。

我有一个API调用,返回4个位置。然后使用breeze,我尝试使用where子句对其进行过滤,如下所示:

  function getLocations(clientId) {
        var self = this;
        return EntityQuery.from('GetLocations')
                            .withParameters({ clientId: clientId })
                            .where("activeStatus", "==", "0")
                            .expand('LocationType')
                            .using(self.manager)
                            .execute()
                            .then(querySucceeded, this._queryFailed);

        function querySucceeded(data) {
            if (data.results.length > 1) {
                locations = data.results;
            }
            return locations;
        }
    }

理想情况下,这应该给我0行,因为在所有4行中,' activeStatus'是1.然而,它仍然显示我所有4个结果。我尝试使用另一个针对locationType的过滤器,它的结果相同。 breeze side where子句不会被执行。

更新以回答问题:

以下是我的控制器中API调用的样子:

public object GetLocations(int clientId) {
}

如您所见,它只接受clientId作为参数,因此我使用with parameter子句。我认为微风会照顾activeStatus where子句,而我不必在后端对其进行过滤。这是错的吗?

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

Breeze文档表明withParameters通常用于非.NET后端或不识别oData URI的服务器。是否有可能因为.withParameters而忽略了where子句?你不能使用clientID过滤器重写where子句吗?

function getLocations(clientId) {
    var self = this;
    var p1 = new breeze.Predicate("activeStatus","==","0");
    var p2 = new breeze.Predicate("clientId","==",clientId);
    var p = p1.and(p2)
    return EntityQuery.from('GetLocations')
                        .where(p)
                        .expand('LocationType')
                        .using(self.manager)
                        .execute()
                        .then(querySucceeded, this._queryFailed);

    function querySucceeded(data) {
        if (data.results.length > 1) {
            locations = data.results;
        }
        return locations;
    }
}

我先试试这个。或者将where子句放在withParameters语句中,具体取决于您的后端。如果这不起作用,那么可能还有其他选择。

祝你好运。

编辑:我使用的一个例子:

这是我查询的API端点:

// GET: breeze/RST_ClientHistory/SeasonClients
        [HttpGet]
        [BreezeQueryable(MaxExpansionDepth = 10)]
        public IQueryable<SeasonClient> SeasonClients()
        {
            return _contextProvider.Context.SeasonClients;
        }

以下是我使用的查询示例:

        // qFilters is object.  Values are arrays or strings, keys are id fields.  SeasonClients might also be Clients

        // Setup predicates
        var p, p1;
        // link up the predicates for passed data
        for (var f in qFilters) {
            var compareOp = Object.prototype.toString.call(qFilters[f]) === '[object Array]' ? 'in' : '==';
            if (!qFilters[f] || (compareOp == 'in' && qFilters[f].length == 0)) continue;
            fLC = f.toLowerCase();
            if (fLC == "countryid") {
                p1 = breeze.Predicate("District.Region.CountryId", compareOp, qFilters[f]);
            } else if (fLC == "seasonid") {
                p1 = breeze.Predicate("SeasonId", compareOp, qFilters[f]);
            } else if (fLC == "districtid") {
                p1 = breeze.Predicate("DistrictId", compareOp, qFilters[f]);
            } else if (fLC == "siteid") {
                p1 = breeze.Predicate("Group.Site.SiteId", compareOp, qFilters[f]);
            } else if (fLC == "groupid") {
                p1 = breeze.Predicate("GroupId", compareOp, qFilters[f]);
            } else if (fLC == "clientid" || fLC == 'seasonclientid') {
                p1 = breeze.Predicate("ClientId", compareOp, qFilters[f]);
            }
            // Setup predicates
            if (p1) {
                p = p ? p.and(p1) : p1;
            }
        }

        // Requires [BreezeQueryable(MaxExpansionDepth = 10)] in controller
        var qry = breeze.EntityQuery
                    .from("SeasonClients")
                    .expand("Client,Group.Site,Season,VSeasonClientCredit,District.Region.Country,Repayments.RepaymentType")
                    .orderBy("DistrictId,SeasonId,GroupId,ClientId");
        // Add predicates to query, if any exist
        if (p) qry = qry.where(p);

        return qry;

这比它需要的时间长,但我想确保一个完整的工作示例在这里。您会注意到没有理由使用.withParameters。只要在服务器上正确设置了Context,链接谓词(where子句)应该可以正常工作。在这种情况下,我们创建where子句,最多10个ANDs过滤使用严格相等或IN集合,具体取决于qFilters对象中传递的内容。

我认为您应该摆脱后端控制器中的参数,使方法无参数,并在您的查询中包含clientId匹配作为附加谓词。

此方法还使您的API端点更加灵活 - 您可以将其用于各种查询,即使ClientId与它们无关。

这有帮助吗?如果您还有其他问题,请告诉我?

相关问题