Breeze:无法通过名称找到“类型”:'人'。请务必先执行查询或调用fetchMetadata

时间:2014-01-10 09:35:06

标签: angularjs breeze

我已从NuGet下载HotTowel.Angular.Breeze并使用breeze api控制器进行设置。

我已配置datacontext.js以创建新经理并创建并公开了一个功能来创建新实体:

function datacontext(emManager) {
    var manager = emManager.newManager();

    var service = {
        create: create,
        save: save
    };

    return service;

    function create(entityName) {
        return manager.createEntity(entityName);
    }

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

        function saveSucceeded(result) {
            // Save Successful
        }

        function saveFailed(error) {
            // Save Failed
        }
    }
}

然后我在我的控制器中这样称呼它:

    function getNewPerson() {
        vm.person = datacontext.create(entityName);
        return vm.person;
    }

如果我直接在浏览器中加载此页面,则会出现以下错误:

Error: Unable to locate a 'Type' by the name: 'Person'. 
Be sure to execute a query or call fetchMetadata first.

但是,如果我在执行查询后导航到该页面,它会正常加载,我可以保存新实体等。

所以我的问题是,在哪里打电话fetchMetadata的好地方?它会出现在entityManagerFactory方法的newManager吗?

    function newManager() {
        var mgr = new breeze.EntityManager({
            serviceName: serviceName,
            metadataStore: metadataStore
        });
        return mgr;
    }

修改

我按照下面@Rob的建议尝试过,但我仍然遇到同样的错误:

    function setupMetadata() {
        var deferred = $q.defer();
        metadataStore.fetchMetadata(serviceName).then(function () {
            deferred.resolve();
        }).fail(function (ex) {
            deferred.reject(ex);
        });

        return deferred.promise;
    }

然后我将setupMetadata();放在app.run内:

app.run(['$route', '$rootScope', '$q', 'entityManagerFactory', function ($route, $rootScope, $q, emFactory) {
    // Include $route to kick start the router.
    breeze.core.extendQ($rootScope, $q);

    emFactory.setupMetadata();
}]);

2 个答案:

答案 0 :(得分:1)

在数据服务传递的顶部是微风并创建一个新的元数据存储

        var servicePath = 'http://localhost/breeze/Blah';
        var metadataStore = new breeze.MetadataStore({
             //Configure Stuff
             //namingConvention: breeze.NamingConvention.camelCase
        });

然后获取您的元数据

        $rootScope.initialized = false;
        function setupMetadata() {
            var deferred = $q.defer();
            metadataStore.fetchMetadata(servicePath).then(function () {
                    $rootScope.initialized = trie;
                    deferred.resolve();
            }).fail(function (exception) {
                    deferred.reject(exception);
            });
            return deferred.promise;
        };

<强>控制器

.controller('SomeCtrl', function($rootScope, $scope) { 
        $rootScope.$watch('initialized', function() {
            //Make initial calls
        });
}

我会公开此方法,以便您可以在引导应用程序或进行身份验证时调用它,以确保在执行任何其他操作之前已加载元数据存储。

答案 1 :(得分:0)

你应该检查第一个元数据然后创建新人,你的创建方法看起来像这样。

function create(entityName) {
        var hasMetadata = checkMetadata(manager, serviceName);
        if (hasMetadata) {
            return manager.createEntity(entityName);
        } else {
            return getMetadata(manager, serviceName).then(createPerson);
        }            
    }

function createPerson()
{
    return manager.createEntity(entityName);
}

//Check wther Metadata is present in the given manager or not
var checkMetadata = function (manager, serviceName) {
    try {
        var result = false;
        if (manager.metadataStore.hasMetadataFor(serviceName)) {
            result = true;
        }
        return result;
    } catch (e) {
        throw e;
    }
};

//Method to get Metadata from a given entityManager and serviceName
this.getMetadata = function (manager, serviceName) {
    var store = manager.metadataStore;
    return store.fetchMetadata(serviceName);
};

我认为它会对你有所帮助。