How to connect to metadataLoaded event (or equivalent) in breeze.js

I am working on a SPA that uses easy access to access data. I want to create an instance of metadata as soon as possible, and I believe that it will be after , this will complete the initialization:

var manager = new entityModel.EntityManager(serviceName); 

However, the entity manager must execute an ajax request for the web api controller to download the metadata, and if I try manager.metadataStore.getEntityType("EntityName") before it finishes, I get:

 Uncaught Error: Unable to locate an 'Type' by the name 

My question is, is there an event that fires when metadata is loaded ? I wandered around the docs, but it seems like I can't find him.

+4
source share
3 answers

There is no event, but you can call the MetadataStore.fetchMetadata method yourself and execute your logic in the "next" part of the promise.

The MetadataStore.fetchMetadata method is automatically called internally by the first request to each server , if it has not already been called. .

So just call MetadataStore.fetchMetadata just before the first request.

Hope this makes sense.

+5
source

Starting with Breeze 1.4.16, MetadataStore now has the metadataFetched event described here: http://www.breezejs.com/sites/all/apidocs/classes/MetadataStore.html#event_metadataFetched

+3
source

The problem with the proposed solution is that fetchMetadata may fail ... for various reasons. If the failure is caused by a short-term communication failure, the next request request retrieves metadata, and the application starts accessing the data without initializing everything that is necessary to initialize the WRT metadata (in our case, client-side checks). In our case, the application works fine, except that there are no checks.

It is odd that this condition occurs a lot. A call to fetchMetadata fails, but subsequent requests fail. It does not happen every time, but more than a little happens.

We could add state to the system that prevents access to other data until fetchMetadata succeeds and retries until it works. But that is a lot of work. The event that fires when the download completes will be much better.

+1
source

All Articles