我想在Breeze中手动保存实体。我们没有选择(尽管我试图争取我的意见)使用SaveChanges(JObject saveBundle)并且需要直接命中具有POST / PUT请求的特定URL的第三方Web API。
所以我基本上循环遍历EntityManager.getChanges(),然后处理Modified,Added和Deleted实体。
我可以处理"修改"没有任何问题。但是,在"添加",我知道我需要在成功保存后添加新实体时更新keyMappings,但无法找到有关如何在JavaScript中手动执行此操作的任何文档。
我还想查看是否有任何返回错误的示例。基本上我想挂钩这个电话:
$http(params).then(
function (response) { // success
console.log(response);
// update key mappings if its an "Added" somehow
// entityAspect.acceptChanges();
dfd.resolve("something eventually");
},
function () { // error
// added error object here and reject changes on this entity? or just show error message?
dfd.reject("error");
});
return dfd.promise;
答案 0 :(得分:0)
如果有人想知道,我只需检查entityAspect.entityState.isAdded()方法。获取从我的第三方返回的新身份,并相应地更新ID。我们的系统更好一点,因为我们为所有实体设置了密钥。
代码明智它看起来像这样(dfd是$ q defer):
$http(params).then(function (response) { // success
// on add update the instance id with the new instance id
if (entityState.isAdded()) {
var newId = response.data.changedInstance.newId;
entity.Id = newId ;
}
entityAspect.acceptChanges();
dfd.resolve(response.data);
},
function (response) { // error
dfd.reject(response.data);
});