展开breeze Entity属性

时间:2013-05-22 15:52:15

标签: asp.net-mvc-4 knockout.js breeze

我对breeze / knockout很新,但我99%的方式去做我需要做的事情。我正在使用Hot Towel模板,我通过微风成功检索了一个项目列表。实体(ITBAL)是数据库的第一个实体框架实体。当我看到Fiddler中回来的JSON时,我看到了正确的数据。问题是data.results的所有属性都是dependentobservables,而不是原始值本身。

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

我试图解开对象,但不断收到循环引用错误。我不知道为什么会这样,因为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);


    }

}

viewmodel的相关部分:

        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属性,它可以正常工作。

1 个答案:

答案 0 :(得分:1)

Jon,删除[Breeze]属性有效地禁用了应用程序的微风,因此我认为这不是您问题的长期答案。

如果你实际上并不想要这个场景的实体 - 你只是想要数据 - 而不是Breeze投影,只提到要在网格中显示的数据似乎是最好的选择。预测会返回未包含在KO可观察量中的原始数据,并且不会保存在Breeze EntityManager缓存中。

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

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

  something.entityAspect.entity //'entity' points back to 'something'

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

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

function success(data) {
    var unwrapped = ko.toJS(data.results).map(
                           function(entity) {
                              delete entity.entityAspect;
                              return entity;
                           });
    vm.itbals(unwrapped);
    vm.totalRecords(1); // huh? What is that "parseInt ..." stuff?
    itemBalancesGrid.mergeData(vm.itbals(), args.pageNum, parseInt(vm.totalRecords()));
})

ko.toJS是一个Knockout函数,它递归地解包对象或对象集合,返回值的副本。然后我们迭代复制的对象图,删除他们的entityAspect属性。结果数组填入vm.itbals可观察量并交付。

您可以想象如何概括这一点以删除任何给您带来麻烦的事情。

附加

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

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

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

function success(data) {
    var unwrapped = ko.toJS(data.results).map(
                           function(entity) {
                              delete entity.entityAspect;
                              return entity;
                           });
    itemBalancesGrid.mergeData(unwrapped, args.pageNum, data.inlineCount);
})