简体   繁体   English

删除扩展项后更新AngularJS绑定和从缓存查询

[英]Updating AngularJS bindings and query from cache after deleting expanded item

Please reference this question as background. 将此问题作为背景。

After running a query that includes an expand to get related entities, if one then deletes one of the expanded entities, how is the breeze cache updated? 运行包含扩展以获取相关实体的查询后,如果随后删除其中一个扩展实体,微风缓存如何更新?

I ask this, because (per the recommended solution to the referenced question), I am finding that if I query from cache after deleting an expanded item, the item still appears unless I force a call to the server. 我之所以这样问,是因为(根据对所提问题的推荐解决方案),我发现如果删除扩展项后从缓存中查询,则除非我强制调用服务器,否则该项仍会出现。

this is the function i'm currently using to retrieve from cache if available or the server if not. 这是我当前用于从缓存中检索(如果有)或从服务器中检索(如果没有)的功能。 I am forcing a call to this function after my delete. 删除后,我将强制调用此函数。

function _getByIdExpanded(resource, id, expand, forceRefresh) {
    var self = this;

    if (!forceRefresh) {
        var entity = self.em.getEntityByKey(resource, id);
        if (entity && entity.isReadyForEdit) {
            //logSuccess('Retrieved ' + resource + ' ' + entity.name + ' from cache', entity, true);
            if (entity.entityAspect.entityState.isDeleted()) {
                entity = null;
            }
            return self.$q.when(entity);
        }
    }

    return eq.from(resource + 's') //pluralise for table name in DB
        .where('id', '==', id)
        .expand(expand)
        .using(self.em).execute()
        .to$q(succeeded, self._failed);

    function succeeded(data) {
        var result = data.results;
        entity = result[0];
        if (!entity) {
            logError('Could not find ' + resource + ' with id: ' + id, null);
            return null;
        }
        entity.isReadyForEdit = true;
        //logSuccess('Retrieved ' + resource + ' ' + entity.name + ' from server', entity, true);
        return entity;
    }
}

EDIT: 编辑:

Ok, after some tests, it appears my error is actually an Angular one not a breeze one. 好吧,经过一些测试,看来我的错误实际上是一个Angular错误而不是轻而易举的错误。

Here are the two calls in my controller that are related: 这是控制器中相关的两个调用:

function deleteEntity(entity) {
    return bsDialog.deleteDialog(entity.name).then(function () {
        datacontext.markDeleted(entity);
        save().then(function () {
            if (entity.entityType === dboardConfigEntity) {
                $location.path('/subscriber/' + subscriberId);
            } else { getDboardConfig(true); } // forcing call back to server for testing
        });
    });
}

function getDboardConfig(forceRefresh) {
    if ($routeParams.newOrExisting === 'new') {
        subscriberId = $routeParams.id;
        return vm.dboardConfig = datacontext.dboardConfigs.create(subscriberId);
    }
    return datacontext.dboardConfigs.getByIdExpanded($routeParams.id, forceRefresh)
        .then(function (data) {
        vm.dboardConfig = data;
        subscriberId = vm.dboardConfig.subscriberId;
        vm.hideChildren = false;
    });
}

This call is just a pass-through to an abstract repository (which contains the function in my first post above): 该调用只是对抽象存储库的传递(该存储库包含上面我的第一篇文章中的函数):

function getByIdExpanded(id, forceRefresh) {
    var expand = 'Dims';
    return this._getByIdExpanded(entityName, id, expand, forceRefresh);
}

This is my delete and save calls in my datacontext: 这是我在数据上下文中的删除和保存调用:

function markDeleted(entity) {
    return entity.entityAspect.setDeleted();
}

function save() {
    return em.saveChanges()
    .to$q(saveSucceeded, saveFailed);

    function saveSucceeded(result) {
        logSuccess('Successfully saved to the server', result, true);
        return result;
    }

    function saveFailed(error) {
        var msg = config.appErrorPrefix + 'Failed to save changes to the server. '
            + breeze.saveErrorMessageService.getErrorMessage(error);
        error.message = msg;
        logError(msg, error);
        throw error;
    }
}

An extract from my markup: 我的标记摘录:

<table class="table table-condensed table-striped" data-ng-show="vm.dboardConfig.dims.length > 0">
    <thead>
        <th>{{dt.name}}</th>
        <th></th>
    </thead>
    <tbody>
        <tr data-ng-repeat="d in vm.dboardConfig.dims | filter:{ dimTypeId: dt.id }">
            <td>{{d.name}}</td>
            <td>
                <div class="pull-right">
                   <a href="" data-ng-click="vm.edit(d)">
                        <i class="fa fa-pencil blue"></i>
                   </a>
                   <a href="" data-ng-click="vm.delete(d)">
                        <i class="fa fa-times-circle red"></i>
                   </a>
                </div>
            </td>
        </tr>
     </tbody>
</table>

As far as I can see, I'm resolving all my promises returned form the query, so why are the elements in my table not updating unless I refresh? 据我所知,我正在解决从查询返回的所有诺言,那么,除非刷新,为什么表中的元素才会更新?


FURTHER EDIT: 进一步编辑:

After running through the browser debugger, what I'm noticing is that even though I have marked the expanded child entity for deletion and saved, when I run the query to retrieve the updated entity, the expanded child is still included, but in a detached state, indicating that breeze has saved the deletion. 在通过浏览器调试器运行之后,我注意到的是,即使我已将展开的子实体标记为删除并保存,当我运行查询以检索更新的实体时,仍然包含了展开的子实体,但是在一个分离的子实体中状态,表示微风已保存删除。 After reviewing some of the Breeze documentation on this, the question then comes down to this - How do I adjust my code so that Angular doesn't bind detached entities? 在查看了有关Breeze的一些文档之后,问题就落到了这个问题上-我该如何调整代码,以便Angular不会绑定分离的实体? My understanding was previously that Angular would not display detached entities, but for some reason it is... 我以前的理解是Angular不会显示分离的实体,但是由于某种原因,它是...

Ok, so after scouring posts on SO, finally came across this one . 好的,因此在搜集有关SO的帖子后,终于遇到了这一点

For those interested, the problem in my case was being caused by Breeze. 对于那些感兴趣的人,在我看来,这个问题是由Breeze引起的。 All my code above seems to be okay. 我上面的所有代码似乎都还可以。

However, my original class design for Dim (my "expanded" entity) was... 但是,我最初为Dim(我的“扩展”实体)设计的课程是...

public class Dim
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int DboardConfigId { get; set; }
    public int DimTypeId { get; set; }
    public DimType DimType { get; set; }
}

The entity had no navigation property back to DboardConfig. 该实体没有回到DboardConfig的导航属性。 Becuase I had no need to navigate to any of DboardConfig's properties, I decided this was unecessary. 因为我不需要导航到DboardConfig的任何属性,所以我认为这是不必要的。 However, as it happens, it appears that lacking the navigation property causes the problem described in my question. 但是,碰巧,似乎缺少导航属性会导致我的问题中描述的问题。 so by adding a property public DboardConfig DboardConfig { get; set; } 因此,通过添加属性public DboardConfig DboardConfig { get; set; } public DboardConfig DboardConfig { get; set; } public DboardConfig DboardConfig { get; set; } to the class, the problem disappeared. public DboardConfig DboardConfig { get; set; }上课,问题就消失了。

I'd be curious to understand why Breeze needs this navigation property for deleting expanded items and having them reflect in the binding? 我很好奇为什么Breeze为什么需要此导航属性来删除扩展的项目并使它们反映在绑定中?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM