Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
breeze EntityManager:如何附加普通javascript对象_Breeze - Fatal编程技术网

breeze EntityManager:如何附加普通javascript对象

breeze EntityManager:如何附加普通javascript对象,breeze,Breeze,我有从我的客户机调用的自定义OData操作。此操作的结果是一个JSON对象列表,这些对象需要作为实体合并回Breeze缓存。如何将JSON对象转换为Breeze实体并将该实体合并回entityManager的缓存?一些代码: $http.post('/odata/MyEntityType/MyCustomAction/', { 'someData': JSON.stringify(element1), 'Som

我有从我的客户机调用的自定义OData操作。此操作的结果是一个JSON对象列表,这些对象需要作为实体合并回Breeze缓存。如何将JSON对象转换为Breeze实体并将该实体合并回entityManager的缓存?一些代码:

   $http.post('/odata/MyEntityType/MyCustomAction/', {
                    'someData': JSON.stringify(element1),
                    'SomeOtherData': JSON.stringify(element2)
                })
                .success(function (results) {
                    //results.value is an array of *MyEntityType* JSON objects.   `//Is there a way to convert these to breeze entities?`

                    });
我尝试过的一些事情:

重要性(“MyEntityType”,即JSONFORANTY)//只是在黑暗中刺伤

createEntity(“MyEntityType”,即JSONFORANTY)//错误:“不允许”的合并策略不允许您在已附加具有相同密钥的实体时附加实体”


我认为您使用了
manager.createEntity
方法。请尝试指定合并策略(默认为“不允许”,这会给您的用例带来问题):

合并策略:

createEntity:

createEntity无法工作 抱歉,杰里米,但这行不通。下面是一个测试,说明了原因:

// Failing test
test("merge customer date into existing cached entity using `createEntity`", function () {
    var em = newEm();

    // Create an unchanged entity in cache as if it had been queried
    em.createEntity('Customer', {
        CustomerID: dummyCustID,
        CompanyName: 'Foo Co',
        ContactName: 'Ima Kiddin'
    }, UNCHGD);  // creates the entity in the Unchanged state

    // Try to merge some changes into that entity using createEntity.
    var changes = {
        CustomerID: dummyCustID,
        CompanyName: 'Bar Co',
    }
    var cust = em.createEntity('Customer', changes, 
                   UNCHGD, breeze.MergeStrategy.OverwriteChanges);

    ok(cust.entityAspect.entityState.isUnchanged(), "cust should be 'Unchanged'");

    // using Knockout; it's simpler if using Angular
    equal(cust.CompanyName(), 'Bar Co', "Company name should be updated by merge'");
    equal(cust.ContactName(), 'Ima Kiddin', "Contact name should be unchanged after merge'");
});
第三个断言失败,因为
createEntity
方法覆盖每个属性,而不仅仅是一个感兴趣的数据值(
CompanyName
),这意味着
ContactName
为空

重要性也不会起作用 出于同样的原因。当您导入实体时,您导入的是整个实体,而不仅仅是其中的一部分。因此,在本例中,这也将删除联系人姓名

手动合并 我认为如果你想把结果和缓存中的实体混合在一起,你必须手工操作,你必须迭代结果并更新缓存的等价物

假设上面的
更改
变量是您发布的结果。以下测试确实通过:

// Passing test
test("merge customer date into existing cached entity", function () {
    var em = newEm();

    // Create an unchanged entity in cache as if it had been queried
    em.createEntity('Customer', {
        CustomerID: dummyCustID,
        CompanyName: 'Foo Co',
        ContactName: 'Ima Kiddin'
    }, UNCHGD);  // creates the entity in the Unchanged state

    // Merge some changes into that entity
    // Imagine that `changes` came from your server as a result of your POST
    var changes = {
        CustomerID: dummyCustID,
        CompanyName: 'Bar Co',
    }

    // First find the customer in cache.
    // Here I'm assuming it will always be there; you should be careful
    var cust = em.getEntityByKey('Customer', changes.CustomerID);

    // using Knockout; it's a little simpler in Angular
    for (var p in changes) { cust[p](changes[p]); }

    cust.entityAspect.setUnchanged(); // cuz the previous updates changed the state to "Modified"

    ok(cust.entityAspect.entityState.isUnchanged(), "cust should be 'Unchanged'");
    equal(cust.CompanyName(), 'Bar Co', "Company name should be updated by merge'");
    equal(cust.ContactName(), 'Ima Kiddin', "Contact name should be unchanged after merge'");
});
功能请求 我可以想象将来,Breeze可以通过所有适当的错误检查为您实现自动化。如果您认为这样的“JSON实体合并”功能对Breeze来说是一个有价值的增强,请将其添加到。我很快会同意,但除非人们需要,否则我们不能添加功能。

顺便说一句,好名字:“what evAR”
// Passing test
test("merge customer date into existing cached entity", function () {
    var em = newEm();

    // Create an unchanged entity in cache as if it had been queried
    em.createEntity('Customer', {
        CustomerID: dummyCustID,
        CompanyName: 'Foo Co',
        ContactName: 'Ima Kiddin'
    }, UNCHGD);  // creates the entity in the Unchanged state

    // Merge some changes into that entity
    // Imagine that `changes` came from your server as a result of your POST
    var changes = {
        CustomerID: dummyCustID,
        CompanyName: 'Bar Co',
    }

    // First find the customer in cache.
    // Here I'm assuming it will always be there; you should be careful
    var cust = em.getEntityByKey('Customer', changes.CustomerID);

    // using Knockout; it's a little simpler in Angular
    for (var p in changes) { cust[p](changes[p]); }

    cust.entityAspect.setUnchanged(); // cuz the previous updates changed the state to "Modified"

    ok(cust.entityAspect.entityState.isUnchanged(), "cust should be 'Unchanged'");
    equal(cust.CompanyName(), 'Bar Co', "Company name should be updated by merge'");
    equal(cust.ContactName(), 'Ima Kiddin', "Contact name should be unchanged after merge'");
});