【问题标题】:How Can I Set Breeze Entity Properties without breeze.EntityAspect when property change属性更改时如何在没有微风.EntityAspect 的情况下设置微风实体属性
【发布时间】:2013-07-04 22:18:52
【问题描述】:

我有用于管理名为“QuestionCategory”的实体的 jqwidget 网格; 我有一个包含所有实体属性的 observableArray moduleQuestions。当我想将从服务器获取的实体或从客户端新创建的实体推送到作为我的网格源的 moduleQuestions 中时,我确实有一个异常说 TypeError: Converting circular structure to JSON。但是,当我删除实体的 entityAspect 时,我成功推送到我的 observableArray。 这是添加新实体的方法。

    createQuestionModule(data, observableModule: KnockoutObservableArray, moduleEntityAspect:KnockoutObservableArray){
    //data object
    //observableModule :moduleQuestions KnockoutObservableArray
//moduleEntityAspect an observableArray to keep track of each entity's entityAspect
                var newQuestionModuleType :any= this.manager.metadataStore.getEntityType("QuestionCategory");
                var newQuestionModule = newQuestionModuleType.createEntity(data);
                var mapper = this.manager.addEntity(newQuestionModule);
                moduleEntityAspect.push(mapper.entityAspect);//obervableArray to hold annoying entityAspect before deleting it
                 delete mapper.entityAspect;
                observableModule.push(mapper);
                observableModule.valueHasMutated();


        }

所以我在将实体推送到我的 observableArray 之前删除了 entityAspect,并且我成功显示了一个网格。这是生成网格的淘汰赛绑定:

 <div data-bind="jqxGrid: {
    source: moduleQuestions,
    autoheight: true,

    //rendertoolbar : '',
    theme: 'bootstrap',
    editable: true,
    selectionmode: 'singlecell',
    altrows:true,
    sortable: true,
    enabletooltips:true,
    pageable: true,
    autoheight: true,
    showemptyrow: false,
    autosavestate:true,
    columns: [

         { text: 'Module Name', dataField: 'CategoryName', width: 100, cellsalign:'center', validation: moduleNameValidation, cellendedit:celledited },
         { text: 'Description', dataField: 'Description', width: 200, cellsalign: 'center', cellendedit: celledited },
         { text: 'Status', dataField: 'IsEnable', width: 100, columntype: 'checkbox', cellsalign: 'center', cellendedit: celledited },
         { text: 'Action', dataField: 'QuestionCategoryID', cellsrenderer: AddSubjectRender, editable: false, cellsalign: 'center', cellendedit: celledited }

    ]}" id="questionModuleGrid">

列数组中有一个cellendit,它调用一个特定的函数celledited,在调用过程中,只要编辑网格中的任何单元格,就会向它传递五个参数。每当更新网格中的单元格时,就会发生这种情况。这是celledited 函数:

 celledited = (row, datafield, columntype, oldvalue, newvalue) => {
        //row is the row index
        //datafield is an entity property from breeze
        // columntype is a type of column which can be checkbox,textbox etc 
        //oldvalue is the original value before being changed
        //newvalue is the _latestValue
        if (oldvalue != newvalue) {
            var breezeEntityinGrid = QuestionCategory.moduleQuestions()[row];// this gives us the whole row that is being edited form observableArray =>moduleQuestions
            var entityAspect = QuestionCategory.moduleEntityAspect()[row];// get the entityAspect of the enity.

            breezeEntityinGrid["entityAspect"] = entityAspect; // to add the entityAspect back to the entity


            //then, this produces exception when updating the particular entity property ()=>TypeError: Converting circular structure to JSON
            breezeEntityinGrid[datafield](newvalue); //so  i commented this out then,  try the following method




            breezeEntityinGrid.Description._latestValue = newvalue; //the property to be updated
            var newQuestionModuleType = this.manager.metadataStore.getEntityType("QuestionCategory");

            this.manager.attachEntity(breezeEntityinGrid, breeze.EntityState.Modified);//yet the manager is not aware of that

            delete breezeEntityinGrid["entityAspect"];//to remove it from the entity

            return true;

        }
    }

我很困惑。我想使用网格并在实体更改时设置它们的属性,以便在调用 saveChages 时可以保存更新的值,但是将其提交给微风管理器,就会出现问题。请问我需要帮助。

【问题讨论】:

    标签: knockout.js breeze single-page-application durandal


    【解决方案1】:

    不完全确定您要完成什么,但删除实体的 entityAspect 是一个非常糟糕的主意。 Breeze 在很大程度上取决于此。一般来说,根据所讨论的网格,还有其他消除圆度的方法。我会与电网供应商交谈并与他们讨论这个问题。

    【讨论】:

    • 非常感谢 Jay Tranband。我非常相信微风在很大程度上取决于这个人(实体方面)。但我只是想摆脱它。我会很高兴,如果我能找到任何与微风完美配合的电网供应商,这样我就可以开始了。再次非常感谢。
    • 循环问题很可能会出现在任何复杂度不高的对象图上;所以这通常不是微风问题。即,即使您删除 entityAspect,您最终也可能会遇到问题。
    • 好吧,这意味着我必须找到处理这种循环的方法。感谢您抽出宝贵时间@Jay Traband
    • 我已经通过使用 Douglas Crockford cycle.js 部分解决了我的问题。我能够摆脱循环问题。但是,如果数据增长超出预期,我仍然需要处理性能问题。
    猜你喜欢
    • 2014-10-01
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    相关资源
    最近更新 更多