Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ajax

breeze

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.

like image 721
Goran Obradovic Avatar asked Jan 03 '13 20:01

Goran Obradovic


2 Answers

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

like image 69
Jay Traband Avatar answered Nov 15 '22 04:11

Jay Traband


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.

like image 27
steve Avatar answered Nov 15 '22 04:11

steve