0

好的,我用另一种方式问了这个问题,但还没有看到回复,所以试图简化问题 -

我想创建多个实体并将它们留在 Breeze 的“添加”实体状态中。然后我只想返回那些对象并通过导航属性将它们绑定回父对象。

var createEntities = function (parent) {
    manager.createEntity('child', {parentId = parent.id()});
};

然后查询父级并仅返回添加视图的创建实体 -

var getThoseDamnedEntities = function (parentObservable) {
        var query = EntityQuery.from('Parents')
            .where('id', '==', parentObservable.id())
            .expand('Child');

        return manager.executeQuery(query)
            .then(querySucceeded)
            .fail(queryFailed);

        function querySucceeded(data) {
            if (parentObservable) {
                parentObservable(data.results);
            }
            log('Retrieved [Parent] from remote data source',
                data, true);
        }
};

但我想通过 entityState.Added 过滤子结果,以便我只返回尚未保存的新实体,以便在保存之前对其进行修改。有什么建议么?

为了解释这一点 - 我可以直接按类型查询实体(即 query = EntityQuery.from('Child').where())并使用构造函数来拥有仅用于“添加”实体的 ko.computed 属性但是我仍然不知道如何查询它们并通过导航属性将它们绑定回父对象(对不起,如果我在那里搞砸了术语:))

4

1 回答 1

1

Assuming that I've understood your question, you actually have several approaches that you can take to this.

First, you should really only be querying the entityManager's local cache, because this is the only place that "Added" entities will be found. They will not yet have been saved, ( hence still marked as 'Added'). You can do this by executing the inverse of the query you listed above against the local cache only.( I'm guessing about your entity type and property names here)

var query = EntityQuery.from('Children')
        .where('parent_id', '==', parentObservable.id())
        .using(FetchStrategy.FromLocalCache).then(...)

which will return a promise, or you can do the same thing synchonously with

var query = EntityQuery.from('Children')
        .where('parent_id', '==', parentObservable.id()) ;
var localChildren = myEntityManager.executeQueryLocally(query);

This will return every entity, in the local cache, that matches your filter, so if you only want the 'Added' ones, you will need to further filter the 'localParents', i.e.

var addedLocalChildren = localChildren.filter(function(p) {
   return p.entityAspect.entityState.isAdded();
})

The other approach, that might be even simpler is based on the idea that if you have already linked the parent entities to the children via a foreign key, then any entities in the entityManager cache will already have their navigation properties resolved. This means that you can simply do:

var children = parentObservable().child;  // 'children' might be a better name for this prop
var addedLocalChildren = children.filter(function(p) {
   return p.entityAspect.entityState.isAdded();
})

Hope this makes sense.

于 2013-05-06T21:38:41.103 回答