How to attach to "metadataLoaded" event (or equivalent) in breeze.js
Asked Answered
B

3

5

I'm working on an SPA which is using breeze for data access. I want to create an instance of metadata as soon as possible, and I suppose that would be after this has completed initialization:

var manager = new entityModel.EntityManager(serviceName);

However, entity manager needs to perform ajax request to web api controller to load metadata, and if I try manager.metadataStore.getEntityType("EntityName") before it is complete, I get:

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

My question is is there an event which is triggered when metadata is loaded? I wandered through documentation but it seems that I'm unable to find it.

Boutwell answered 3/1, 2013 at 20:36 Comment(0)
B
5

There is no event, but you can call the MetadataStore.fetchMetadata method yourself and perform your logic in the 'then' part of the promise.

The MetadataStore.fetchMetadata method is automatically called internally by the first query against each server, if it hasn't already been called..

So simply call MetadataStore.fetchMetadata yourself before the first query.

Hope this makes sense.

Balbo answered 3/1, 2013 at 21:24 Comment(0)
B
3

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

Balbo answered 14/7, 2014 at 20:5 Comment(0)
B
1

The problem with the proposed solution is that fetchMetadata can fail ... for various reasons. If the failure is due to transient communication failure, then the next query call fetches the metadata and the app will start accessing data without having initialized whatever it needed to initialize WRT the metadata (in our case client-side validations). In our case, the app seems to work fine except there are no validations.

It's odd that this condition happens alot. The call to fetchMetadata fails, but subsequent queries succeed. Does not happen every time, but happens more than a little.

We could add a state to the system that prevents other data access until the fetchMetadata succeeds and retry it until it does succeed. But, that's alot of work. An event that fires when the load completes would be so much better.

Balaton answered 9/12, 2013 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.