0
votes

I'm following John Papa's brilliant pluralsight course on AngularJS/Breeze SPA apps (part 1) and making mods to my particular situation.

I have reference data for Products and Uoms, which are meant for dropdown lists. I want to load these from breeze local cache versus hitting the server all the time. I can retrieve them from the server, but I can't figure out how to query them later from breeze.

The data is exposed server-side as lists of id/name pair objects which are not part of the metadataStore exposed by Entity Framework. I add these entity types to breeze before the call to the server for data.

Here's how I created the unmapped EntityTypes:

  createReferenceEntity('Products');
  createReferenceEntity('Uoms');

  function createReferenceEntity(entityName) {

    // add reference/lookup client-side only entity type
    var et = new breeze.EntityType({
      shortName: entityName,
      namespace: "Model"
    });

    et.addProperty(new breeze.DataProperty({
      name: "id",
      dataType: breeze.DataType.Int32,
      isNullable: false,
      isPartOfKey: true,
    }));

    et.addProperty(new breeze.DataProperty({
      name: "name",
      dataType: breeze.DataType.String,
      isNullable: false,
    }));

    metadataStore.addEntityType(et);
  }

Here's what I have in my Breeze controller:

    [HttpGet]
    public object Lookups()
    {
        // List<ReferenceObject> which is an Id and a Name
        var Products = _repository.Products; 
        var Uoms = _repository.Uoms;
        return new { Products, Uoms };
    }

So I call into the controller from javascript with:

    return EntityQuery.from('Lookups').using(manager)
    .execute().then(querySucceeded, _queryFailed);

The query succeeds. What I see in the FireBug Net window looks to be ok. So the data is coming across the wire.

So what I cannot seem to be able to do is to then query the lookup data again. Is it not supposed to be in the Breeze cache? This is what I am trying. Comes back as empty:

    _getReferenceData('Products', 'name');

    function _getReferenceData( resource, ordering) {
      return EntityQuery.from(resource)
             .orderBy(ordering)
             .using(manager)
             .executeLocally();
    }

Maybe the server is not providing the serialized object across the wire in the right format? I'm not querying breeze properly?

I'd really appreciate any advice here, as I'm getting little bleary eyed trying to figure this out.

Thanks! Corey.

2

2 Answers

0
votes

I think you need to call setEntityTypeForResourceName:

metadataStore.setEntityTypeForResourceName("Products", "Product:#Model");
metadataStore.setEntityTypeForResourceName("Uoms", "Uom:#Model");

Then the EntityManager can know that, when you use the "Products" resource name, it should query "Product" entities. Normally Breeze gets this information from the metadata returned by the server, but since you create the EntityTypes locally, you'll need to provide the info.

1
votes

I noticed first that you probably want to add the defaultResourceName to each type definition. The defaultResourceName is the primary path for retrieving the type from the database. It's what Breeze uses when manually load a navigation path such as order.entityAspect.loadNavigationPath('OrderDetails'). Typically that is the plural of the entity name (e.g., "Products" for an entity named "Product").

Then I saw in your comment that you are setting the path ("Products") for the type ("Products:#Model") which is good but not the same thing (setEntityTypeForResourceName defines a resource path but not the default resource path).

Then I realized that your type name is Products plural, not Product singular. Is that right? That is certainly not conventional.

I'm betting that your server is serializing the entity type name as "Product" ... which is not the name of one of your types.

We could be more sure of an answer if we saw the JSON serialized form of one of the entities from your lookups query. You can copy it from the "Network" tab of your browser developer tools and paste it into your answer. Thanks.