BreezeJS:服务器添加的对象显示在保存更改后添加到客户端中

时间:2013-05-30 14:35:03

标签: breeze

我有一个breeze控制器,在客户端未提交的保存更改期间添加实体。

        protected override bool BeforeSaveEntity(EntityInfo entityInfo)
    {
        if (entityInfo.Entity.GetType() == typeof(User))
        {
            if (entityInfo.EntityState == EntityState.Added)
            {
                User user = entityInfo.Entity as User;
                OrganizationUser orgUser = new OrganizationUser()
                {
                    Enabled = true,
                    OrganizationId = User.OrgId,
                    User = user
                };
                user.OrganizationUsers.Add(orgUser);
            }
            return true;
        }
        throw new InvalidOperationException("You can not use this service to modify an entity of type: " + entityInfo.Entity.GetType().Name);
    }

当返回响应时,客户端breeze管理器将服务器端添加对象的状态设置为“已添加”。上面的OrganizationUser对象最终在客户端上添加了一个状态。然后在下一个SaveChanges中提交。这是一个错误吗?

以下是第一次保存的回复:

{
"$id": "1",
"$type": "Breeze.WebApi.SaveResult, Breeze.WebApi",
"Entities": [{
    "$id": "2",
    "$type": "LeonardoMD.Server.Api.Security.Admin.User, LeonardoMD.Server",
    "Id": 9176,
    "Email": "SearchProviderA@leonardoMD.com",
    "FirstName": "SearchProviderA",
    "LastName": "SearchProviderA",
    "OrganizationUsers": [{
        "$id": "3",
        "$type": "LeonardoMD.Server.Api.Security.Admin.OrganizationUser, LeonardoMD.Server",
        "UserId": 9176,
        "User": { "$ref": "2" },
        "OrganizationId": 421,
        "Enabled": true
    }]
}],
"KeyMappings": [{
    "$id": "4",
    "$type": "Breeze.WebApi.KeyMapping, Breeze.WebApi",
    "EntityTypeName": "LeonardoMD.Server.Api.Security.Admin.User",
    "TempValue": -1,
    "RealValue": 9176
}]

}

以下是第二次保存的提交:

{
"entities": [{
    "Id": -2,
    "CreateDate": null,
    "CreateUserId": null,
    "ModifyDate": null,
    "ModifyUserId": null,
    "Email": "SearchProviderB@leonardoMD.com",
    "FirstName": "SearchProviderB",
    "LastName": "SearchProviderB",
    "BirthDate": null,
    "GenderId": null,
    "AddressLine1": null,
    "AddressLine2": null,
    "City": null,
    "State": null,
    "StateId": null,
    "PostalCode": null,
    "CountryId": null,
    "DayPhone": null,
    "DayPhoneExtension": null,
    "Password": null,
    "SecretQuestion": null,
    "SecretAnswer": null,
    "Enabled": null,
    "AcceptTermsDate": null,
    "entityAspect": {
        "entityTypeName": "User:#LeonardoMD.Server.Api.Security.Admin",
        "defaultResourceName": "Users",
        "entityState": "Added",
        "originalValuesMap": {},
        "autoGeneratedKey": {
            "propertyName": "Id",
            "autoGeneratedKeyType": "Identity"
        }
    }
},
 {
     "UserId": 9176,
     "OrganizationId": 421,
     "Enabled": true,
     "entityAspect": {
         "entityTypeName": "OrganizationUser:#LeonardoMD.Server.Api.Security.Admin",
         "defaultResourceName": "OrganizationUsers",
         "entityState": "Added",
         "originalValuesMap": {},
         "autoGeneratedKey": null
     }
 }],
"saveOptions": {}

}

请注意,第二个实体是先前保存更改的响应中返回的实体。它的entityState设置为Added。

我有一个解决方法,但它很脆弱,并且需要在保存后服务器返回新实体的每种情况下特别编写。有没有办法将Breeze设置为从服务器返回的所有新实体的acceptChanges作为对saveChanges调用的响应?

                manager.saveChanges()
                    .then(function (saveResult) {
                        $.each(saveResult.entities, function (i, entity) {
                            if (entity.organizationUsers && entity.organizationUsers().length > 0)
                                $.each(entity.organizationUsers(), function (index, orgUser) {
                                    orgUser.entityAspect.acceptChanges();
                                });
                            entity.entityAspect.acceptChanges();
                        });
                        dfd.resolve();
                    })
                    .fail(function (error) { _this.handleError(context, promise, error); });

2 个答案:

答案 0 :(得分:3)

我们能够重现问题,这是一个错误。 (在服务器上添加的实体应该作为Unchanged实体返回到客户端) 我们正在努力修复。

=== EDIT ===

为每个要保存的实体调用一次BeforeSaveEntity方法,并且只应操纵相关实体。您可以在http://www.breezejs.com/documentation/custom-efcontextprovider找到更多相关信息。

如果要创建更多要保存在服务器中的实体,则应在BeforeSaveEntities方法中执行此操作,您也可以将其添加到saveMap字典中,以确保它们保存在数据库中。

protected override Dictionary<Type, List<EntityInfo>> BeforeSaveEntities(Dictionary<Type, List<EntityInfo>> saveMap) {
    Dictionary<Type, List<EntityInfo>> saveMapAdditions = new Dictionary<Type, List<EntityInfo>>();

    var prod = this.CreateEntityInfo(product, EntityState.Added);
    // add data to the entity
    saveMapAdditions[typeof(Product)].Add(prod);

    return base.BeforeSaveEntities(saveMap);
}

答案 1 :(得分:2)

截至Breeze 1.3.5,现已上市,已经修复。并感谢你们... ...