2013-05-30 15 views
2

我有一個breeze控制器,在客戶端未提交保存更改期間添加實體。BreezeJS:服務器添加的對象顯示爲保存更改後在客戶端添加的對象

 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); 
    } 

當返回響應時,客戶端微風管理器將服務器端添加對象的狀態設置爲「已添加」。上面的OrganizationUser對象以客戶端上的Added狀態結束。然後它會在下一次SaveChanges中提交。這是一個錯誤?

這裏是從第一個響應保存:

{ 
"$id": "1", 
"$type": "Breeze.WebApi.SaveResult, Breeze.WebApi", 
"Entities": [{ 
    "$id": "2", 
    "$type": "LeonardoMD.Server.Api.Security.Admin.User, LeonardoMD.Server", 
    "Id": 9176, 
    "Email": "[email protected]", 
    "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": "[email protected]", 
    "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設置爲接受從服務器返回的所有新實體的ChangeChanges作爲對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); }); 

回答

3

我們能夠重現問題,它是一個錯誤。 (在服務器上添加的實體應該已作爲未更改的實體返回給客戶端) 我們正在修復。

===才能將其保存每個實體編輯===

的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); 
} 
+0

我想讓你知道,這個bug已經修復,並將在Breeze的巢版本中提供。 – sbelini

+1

另外,請注意,由於您正在服務器上創建新的實體,因此您應該在BeforeSaveEntities中實現它。在您的特定示例中,新實體進入保存狀態,因爲它恰好與BeforeSaveEntity方法的實體相關,但您不應該依賴它。 – sbelini

+0

你能提供一個這樣的例子嗎?也許可以解釋一下爲什麼你應該這樣做? (或者只是指出一些解釋和演示這個的東西。) – StewartArmbrecht

2

作爲微風1.3.5的,現在可用的,這已被固定。並感謝您的回報...

相關問題