【问题标题】:Saving Breeze entities manually and updating keyMappings手动保存 Breeze 实体并更新关键映射
【发布时间】:2014-06-14 01:27:40
【问题描述】:

我想在 Breeze 中手动保存实体。我们只是没有选择(尽管我试图为我的意见而战)使用 SaveChanges(JObject saveBundle) 并且需要直接点击具有特定 URL 的 3rd 方 Web API 以进行 POST/PUT 请求。

所以我基本上是通过 EntityManager.getChanges() 循环,然后处理修改、添加和删除的实体。

我可以毫无问题地处理“修改”。但是,在“已添加”中,我知道在成功保存后添加新实体时需要更新 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;

【问题讨论】:

    标签: javascript angularjs breeze


    【解决方案1】:

    如果有人想知道,我只检查 entityAspect.entityState.isAdded() 方法。获取从我的第 3 方返回的新身份,并相应地更新 id。我们的系统稍微好一点,因为我们为所有实体设置了一个密钥。

    代码看起来像这样(dfd 是 $q 延迟):

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

    【讨论】:

      猜你喜欢
      • 2015-06-01
      • 1970-01-01
      • 2017-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-08
      • 1970-01-01
      相关资源
      最近更新 更多