Question

When I run a simple Angular page using the new breeze angular service with three select queries in it, I see that breeze is requesting three copies of metadata. Is there a way I can cut out the two extra metadata calls?

I create one instance of EntityManager and then issue three queries.

var entityManager = entityManagerFactory.newManager();

    var query = breeze.EntityQuery
      .from("Customers")
      .expand("Orders")
      .orderBy('FName, LName');

    getSet(query, 'my.customers');

    query = breeze.EntityQuery
      .from("Items")
      .orderBy('ItemID');

    getSet(query, 'my.items');

    query = breeze.EntityQuery
      .from("Orders")
      .expand("Details")
      .orderBy('DropDate');

    getSet(query, 'my.orders');

    function getSet(query,setName) {
      entityManager.executeQuery(query)
      .then(function (data) {
        var model = $parse(setName);
        model.assign($scope, data.results);
      })
      .catch(failed)
      .finally(refreshView);
    }

When I trace with Fiddler, I see three metadata returned first followed by three queries.

EDIT --------------------------------------------------------------

Prefetching the metadata before the queries solved the problem. Now, there is only single request.

function getMetadata(manager) {
      manager.fetchMetadata()
        .then(getSets);
}
Was it helpful?

Solution

I'm guessing, that you are getting three metadata fetches in a row because you started three queries simultaneously without waiting for the first to finish.

The first query ( or direct metadata fetch ) within breeze loads the EntityManager's metadata. Any subsequent queries will not fetch metadata.

What you are doing, I think, is asking breeze to perform multiple queries before the metadata for the first has ever returned. Because each query is checking for metadata, which hasn't yet been returned from the server, each submits it's own metadata fetch request. Hence, three metadata fetch requests.

You can avoid this by either prefetching the metadata, via EntityManager.fetchMetadata or by submitting a first query, and then you need to defer any other queries until the promise returned is resolved.

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