0

我对微风/淘汰赛很陌生,但我已经完成了我需要做的事情的 99%。我正在使用 Hot Towel 模板,并且通过微风成功检索项目列表。实体 (ITBAL) 是数据库第一个实体框架实体。当我查看 Fiddler 中返回的 JSON 时,我看到了正确的数据。问题是 data.results 的所有属性都是依赖的可观察对象,而不是原始值本身。

我们有一个自定义网格控件,它试图显示 data.results 数组。因为它不期望观察值,所以它只是显示“函数相关可观察”而不是值。

我试图打开对象,但不断收到循环引用错误。我不知道为什么会这样,因为 ITBAL 与任何东西都没有关联。

Fiddler 报告的数据:

[{"$id":"1","$type":"WebUIHtml5HotTowel.Models.ITBAL, WebUIHtml5HotTowel","IBITNO":"A100                       ","IBWHID":"1 ","IBITCL":"50","IBITSC":"3 ","IBSUSP":" ","IBVNNO":"100   ","IBPRLC":"        ","IBSCLC":"        ","IBCCCD":"P","IBPICD":" ","IBSAFL":"Y","IBSTCS":399.99000,"IBUSCS":0.00000,"IBAVCS":414.95214,"IBLCST":7.00000,"IBLCCC":20.0,"IBLCDT":110923.0,"IBLSCC":20.0,"IBLSDT":130111.0,"IBLXCC":19.0,"IBLXDT":990102.0,"IBMXO1":2100.000,"IBMXO2":0.000,"IBMXO3":0.000,"IBMNO1":5.000,"IBMNO2":0.000,"IBMNO3":0.000,"IBFOQ1":0.000,"IBFOQ2":0.000,"IBFOQ3":0.000,"IBOHQ1":327.000,"IBOHQ2":0.000,"IBOHQ3":0.000,"IBAQT1":1576.000,"IBAQT2":0.000,"IBAQT3":0.000,"IBBOQ1":50.000,"IBBOQ2":0.000,"IBBOQ3":0.000,"IBPOQ1":448.000,"IBPOQ2":0.000,"IBPOQ3":0.000,"IBIQT1":1446.000,"IBIQT2":0.000,"IBIQT3":0.000,"IBRMD1":10.000,"IBRMD2":0.000,"IBRMD3":0.000,"IBRYD1":10.000,"IBRYD2":0.000,"IBRYD3":0.000,"IBISM1":0.000,"IBISM2":0.000,"IBISM3":0.000,"IBISY1":0.000,"IBISY2":0.000,"IBISY3":0.000,"IBAMD1":0.000,"IBAMD2":0.000,"IBAMD3":0.000,"IBAYD1":0.000,"IBAYD2":0.000,"IBAYD3":0.000,"IBMMD1":0.000,"IBMMD2":0.000,"IBMMD3":0.000,"IBMYD1":0.000,"IBMYD2":0.000,"IBMYD3":0.000,"IBSMD1":1.0,"IBSMD2":0.0,"IBSMD3":0.0,"IBSYD1":1.0,"IBSYD2":0.0,"IBSYD3":0.0,"IBBLME":335.000,"IBBLYO":2680.000,"IBBLLY":1441.000,"IBNMTY":8.0,"IBNMLY":11.0,"IBQSMD":21.000,"IBQSYD":21.000,"IBQSLY":20.000,"IBISMD":16318.19,"IBISYD":16318.19,"IBISLY":45714.87,"IBCSMD":373.46,"IBCSYD":373.46,"IBCSLY":67.00,"IBDQMD":0.000,"IBDQYD":0.000,"IBDQLY":0.000,"IBDSMD":0.00,"IBDSYD":0.00,"IBDSLY":0.00,"IBDCMD":0.00,"IBDCYD":0.00,"IBDCLY":0.00,"IBNOMD":18.0,"IBNOYD":18.0,"IBNOLY":18.0,"IBPKMD":15.0,"IBPKYD":15.0,"IBPKLY":14.0,"IBINUS":"        ","IBIAID":0.0,"IBSAID":0.0,"IBCQT1":1527.000,"IBCQT2":0.000,"IBCQT3":0.000,"IBFCST":"Y","IBDRSH":" ","IBWMIU":"JP","IBFL15":"               ","IBUS20":"                    ","IBLPR1":0.00000,"IBLPR2":0.00000,"IBLPR3":0.00000,"IBLPR4":0.00000,"IBLPR5":0.00000,"IBLPCD":" ","IBABCC":"B","IBPRCL":0.0,"IBQBCL":"   ","IBACDC":"Y","IBTDCD":" ","IBDOUM":"   ","IBTP01":0.0,"IBTP02":0.0,"IBTP03":0.0,"IBTP04":0.0,"IBLMCC":20.0,"IBLMDT":130513.0,"IBTMPH":"Y","IBCOMC":" ","IBCOMF":0.00000,"IBITCT":"    ","IBEOQT":0.000,"IBITCM":0.0,"IBBRVW":" ","IBPTID":"  ","IBQTLT":0.0000,"IBCTY1":"AUS","IBCTY2":"AUS","IBTXCD":"1","IBREVS":"Y","IBITXC":"     ","IBMNOQ":0.000,"IBSTUS":0.000,"IBUS30":"                              ","IBPSLN":" ","IBPLIN":"N","IBUPDP":"Y","IBDFII":"2011-08-11T00:00:00.000","IBLHRK":"A","IBPLNC":"          "}]

我的控制器:

    [BreezeController]
public class ItemInquiryController : ApiController
{

    readonly EFContextProvider<AplusEntities> _contextProvider = new EFContextProvider<AplusEntities>();

    [System.Web.Http.HttpGet]
    public string Metadata()
    {
        return _contextProvider.Metadata();
    }

    [HttpGet]
    public IQueryable<ITBAL> ItemBalances(string itemNumber, string warehouse)
    {
        return _contextProvider.Context.ITBALs.Where(i => i.IBITNO == itemNumber && i.IBWHID == warehouse)
                               .OrderBy(i => i.IBWHID)
                               .ThenBy(i => i.IBITNO);


    }

}

来自视图模型的相关部分:

        var manager = new breeze.EntityManager("api/ItemInquiry");
    var store = manager.metadataStore;

    var itbalInitializer = function (itbal) {
        itbal.CompositeKey = ko.computed(function () {
            return itbal.IBITNO() + itbal.IBWHID();
        });
    };

    store.registerEntityTypeCtor("ITBAL", null, itbalInitializer);
    var index = "0" + (args.pageNum * args.pageSize);

    var query = new breeze.EntityQuery("ItemBalances")
        .withParameters({ itemNumber: "A100", warehouse: "1" })
        .take(args.pageSize);

    if (index > 0) {
        query = query.skip(index);
    }

    manager.executeQuery(query).then(function (data) {

        vm.itbals.removeAll();

        var itbals = data.results;//[0].Data;
        itbals.forEach(function (itbal) {
            vm.itbals.push(itbal);
        });

        vm.totalRecords(1);
        itemBalancesGrid.mergeData(vm.itbals(), args.pageNum, parseInt(vm.totalRecords()));

    }).fail(function (e) {
        logger.log(e, null, loggerSource, true, 'error');
    });

我想我一定错过了一些相当简单的东西,但它正在逃避我。

更新:我从 ApiController 中删除了 BreezeController 属性,它可以正常工作。

4

1 回答 1

1

乔恩,删除该[Breeze]属性会有效地为您的应用程序禁用微风,所以我认为这不是您问题的长期答案。

如果您实际上并不需要此场景的实体(您只需要数据),那么仅提及要在网格中显示的数据的 Breeze 投影似乎是最佳选择。投影返回未包含在 KO observables 中且未保存在 BreezeEntityManager缓存中的原始数据。

如果您希望将数据作为缓存实体,并且希望将它们显示在不喜欢 KO 可观察属性的网格中……请继续阅读。

您可以使用ko.toJS. 但是,网格可能会抱怨循环引用(或像某些网格那样抛出“内存不足”异常)......即使实体没有循环导航路径。困难源于这样一个事实,即每个 Breeze 实体都通过其entityAspect属性具有固有的循环性:

  something.entityAspect.entity //'entity' 指向'something'

因为您将 Knockout 用于您的模型库并且因为您说ITBAL没有导航属性(“与任何东西无关”),所以我认为以下内容对您有用:

manager.executeQuery(query).then(success) ...

功能成功(数据){
    var unwrapped = ko.toJS(data.results).map(
                           功能(实体){
                              删除 entity.entityAspect;
                              返回实体;
                           });
    vm.itbals(未包装);
    vm.totalRecords(1); // 嗯?什么是“parseInt ...”的东西?
    itemBalancesGrid.mergeData(vm.itbals(), args.pageNum, parseInt(vm.totalRecords()));
})

ko.toJS是一个 Knockout 函数,它递归地解开一个对象或对象集合,返回值的副本。然后我们遍历复制的对象图,删除它们的entityAspect属性。结果数组被填充到vm.itbalsobservable 中并被传递。

您可以想象如何将其概括为消除任何给您带来麻烦的东西。

额外的

到底是vm.totalRecords什么?我感觉这应该是分页前匹配记录的总数。您可以通过添加.inlineCount()到 Breeze 查询定义中来从 Breeze 中获得它。查询从data.inlineCount属性返回后,您将获得该值。

你真的需要vm.itbals()吗?如果您在这里所做的只是将值传递给网格,为什么不这样做并去掉中间人呢?

下面的成功回调结合了这些想法

功能成功(数据){
    var unwrapped = ko.toJS(data.results).map(
                           功能(实体){
                              删除 entity.entityAspect;
                              返回实体;
                           });
    itemBalancesGrid.mergeData(unwrapped, args.pageNum, data.inlineCount);
})
于 2013-05-22T21:14:23.407 回答