Breezejs EntityManager MetadataStore和fetchEntityByKey [英] Breezejs EntityManager MetadataStore and fetchEntityByKey

查看:78
本文介绍了Breezejs EntityManager MetadataStore和fetchEntityByKey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SPA应用程序(durandaljs),并且有一条特定的路线,可以在其中映射要获取的实体的 id。

I have a SPA application (durandaljs), and I have a specific route where I map the "id" of the entity that I want to fetch.

模板为 /#/ todoDetail /:id。

The template is "/#/todoDetail/:id".

例如, /#/ todoDetail / 232或 /#/ todoDetail / 19。

For example, "/#/todoDetail/232" or "/#/todoDetail/19".

在viewmodel的激活功能上,我获得了路线信息,因此可以获取ID。然后,我创建一个breezejs EntityManager的新实例,以获取具有给定ID的实体。

On the activate function of viewmodel, I get the route info so I can grab the id. Then I create a new instance of breezejs EntityManager to get the entity with the given id.

问题是当我调用manager.fetchEntityByKey( Todos,id)时,EntityManager还没有来自服务器的元数据,因此会抛出异常无法通过名称 Todos找到类型。

The problem is when I call manager.fetchEntityByKey("Todos", id), the EntityManager doesn't have yet the metadata from the server, so it throwing exception "Unable to locate an 'Type' by the name: Todos".

只有在调用fetchEntityByKey之前首先对商店执行查询(manager.executeQuery)时,此方法才有效。

It only works if first I execute a query against the store (manager.executeQuery), prior to calling fetchEntityByKey.

这是预期的行为还是错误?在EntityManager的实例化期间,有什么方法可以自动转染元数据吗?

Is this an expected behavior or a bug ? Is there any way to auto-fecth the metadata during instantiation of EntityManager ?

注意:我认为在我的情况下很难使用共享的EntityManager,因为我想允许用户在浏览器中直接输入路由。

note: I believe it's hard to use a shared EntityManager in my case, because I want to allow the user directly type the route on the browser.

编辑:作为一种临时解决方法,我正在这样做:

EDIT: As a temporary workaround, I'm doing this:

BreezeService.prototype.get = function (id, callback) {
    var self = this;

    function queryFailed(error) {
        app.showMessage(error.message);
        callback({});
    }

    /* first checking if metadatastore was already loaded */

    if (self.manager.metadataStore.isEmpty()) {
        return self.manager.fetchMetadata()
        .then(function (rawMetadata) {
            return executeQuery();
        }).fail(queryFailed);
    } else {
        return executeQuery();
    }

    /* Now I can fetch */
    function executeQuery() {
        return self.manager.fetchEntityByKey(self.entityType, id, true)
                        .then(callback)
                        .fail(queryFailed);
    }
};


推荐答案

您已经了解了 fetchMetadata 。那很重要如果您的应用程序可以在不发出查询的情况下开始运行,则必须使用 fetchMetadata 并等待其返回,然后才能直接在缓存上执行任何操作(例如,检查是否存在

You've learned about fetchMetadata. That's important. If you application can begin without issuing a query, you have to use fetchMetadata and wait for it to return before you can perform any operations directly on the cache (e.g., checking for an entity by key in the cache before falling back to a database query).

但是我感觉到发生了其他事情,因为您提到了多个管理器。默认情况下,新管理员不知道其他任何管理员的元数据。但是您知道吗您可以在管理人员之间共享一个metastore??您可以。

But I sense something else going on because you mentioned multiple managers. By default a new manager doesn't know the metadata from any other manager. But did you know that you can share a single metadataStore among managers? You can.

我经常做的事情(您会在DocCode示例的元数据测试中看到它),是获取应用程序的metadataStore,编写EntityManager工厂函数,该函数使用metadataStore创建新的经理,然后在每次我创建新经理时都使用工厂...就像您在启动ViewModel来查看TodoDetail时所做的那样。

What I often do (and you'll see it in the metadata tests in the DocCode sample), is get a metadataStore for the application, write an EntityManager factory function that creates new managers with that metadataStore, and then use the factory whenever I'm making new managers ... as you seem to be doing when you spin up a ViewModel to review the TodoDetail.

这篇关于Breezejs EntityManager MetadataStore和fetchEntityByKey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆