I have the following code that went from this:

        var getUserByGuid = function (guid, entityObservable) {
        return datacontext.manager.user.fetchEntityByKey('User', guid, true)
                        .then(fetchSucceeded)
                        .fail(queryFailed);

        function fetchSucceeded(data) {
            var entity = data.entity;
            if (ko.isWriteableObservable(entityObservable))
                entityObservable(entity);
            return entity;
        }

        function queryFailed(error) {
            logger.error(error);
        }
    };

to this:

        var getUserByGuid = function (guid, entityObservable) {
        if (datacontext.manager.user.metadataStore.isEmpty()) {
            return datacontext.manager.user.metadataStore.fetchMetadata('breeze/user')
                .then(function () {
                    return datacontext.manager.user.fetchEntityByKey('User', guid, true)
                        .then(fetchSucceeded)
                        .fail(queryFailed);
                });
        } else {
            return datacontext.manager.user.fetchEntityByKey('User', guid, true)
                        .then(fetchSucceeded)
                        .fail(queryFailed);
        }

        function fetchSucceeded(data) {
            var entity = data.entity;
            if (ko.isWriteableObservable(entityObservable))
                entityObservable(entity);
            return entity;
        }

        function queryFailed(error) {
            logger.error(error);
        }
    };

Notice the extra check to verify metadataStore is ready? Since I am making a call to fetch, I would assume this check would happen internally but for some reason it is not.

My code runs well with the following "work-around" in place but wanted to bring this to light.

有帮助吗?

解决方案

Updated 3/1/2014

As of Breeze 1.4.9 (or later), available now this has been fixed.

Previous post

I think you are right. The problem, I think, is that fetchEntityByKey doesn't actually have to perform a fetch when you tell it to search the local cache first. But in this case, if you don't have metadata then the localQuery fails. I'll try to get this fixed in the next release, probably out later this week.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top