Question

I have 2 entity managers:

var mgr1 = new breeze.EntityManager('api/app');
var mgr2 = new breeze.EntityManager('api/app');

Right now I am getting the metadata for each one separately, although the metadata is exactly the same. I am calling the fetch method explicitly to control the timing of when the metadata is loaded.

mgr1.fetchMetadata();
mgr2.fetchMetadata();

I've read that I can share the metadata between the 2 managers but I have not found an example. From what I've read, I think I can specify the metadata in the constructor of the 2nd manager that references the 1st manager's metadata, but not sure what that would look like. So my code would look something like this:

var mgr1 = new breeze.EntityManager('api/app');
mgr1.fetchMetadata();
var mgr2 = new breeze.EntityManager({ serviceName: 'api/app', metadata: WHAT_GOES_HERE});

I know I will also have to sequnece this with promises so the 2nd manager isn't constructed before the 1st managers has it's metadata loaded.

Am I on the right path with this? My goal is to eliminate the extra bandwidth to load the metadata for the 2nd manager. thanks

Was it helpful?

Solution 2

I have never used breeze.js before, but from what I gather from the documentation (http://www.breezejs.com/sites/all/apidocs/classes/EntityManager.html), something like this should work:

var mgr1 = new breeze.EntityManager('api/app');
mgr1.fetchMetadata();
var mgr2 = new breeze.EntityManager({
   serviceName: 'api/app', 
   metadataStore: mgr1.metadataStore
});

Of course mgr2 should be set up after the mgr1.fetchMetadata promise is fulfilled, as you already say in your question.

OTHER TIPS

You don't actually have to fetch the metadata to share the same MetadataStore. The following two statements are a pretty crisp approach:

var em1 = new breeze.EntityManager('api/app'); 
var em2 = em1.createEmptyCopy();

I'm not trying to be clever. My point is that a MetadataStore, which is a container of metadata, is available immediately after EntityManager creation and is well defined prior to holding any metadata whatsoever.

The createEmptyCopy() method "clones" the manager without copying its entity cache contents. The copied attributes include the manager's MetadataStore and its DataService.

Because the managers share the same MetadataStore, fetching metadata with either manager will do the trick.

Check out the Breeze API documentation for EntityManager and MetadataStore.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top