Search code examples
javascriptnode.jsbreeze

breeze get a collection of entities not objects


Using a restful node.js server and sending a collection of entities wrapped in one entity object (Lookups) everything works, but the entities in the collection are seen as plain objects and not entities by breeze. Is there a way to fix this, also instead of using an entity for the Lookups is it possible to use a complex object since its used just as a holder and nothing more. Thanks for the help

The response from the server is in this format

[
  oranges:[{id:1, name:'juicy'}, {id:2, name:'no seeds'}],
  apples:[{id:1, name:'red'}, {id:2, name:'green'}]
]

using this query:

breeze.EntityQuery.from('users/lookups')
            .using(this.manager).execute()
            .then(this.querySucceeded)
            .catch(this.queryFailed);

Here is the metadata code for the lookup

var entityType = {
   name: this.entityNames.Lookup,
   defaultResourceName: 'users/lookups',
   autoGeneratedKeyType: breeze.AutoGeneratedKeyType.Identity,
   dataProperties: {
      lookupID: {dataType: DT.Int32, isPartOfKey: true}
   },
   navigationProperties:  {
      apples: {
             entityTypeName: this.entityNames.Apple
       },
      orange: {
             entityTypeName: this.entityNames.Orange
       }
   }
};

The meta for the Orange and Apple

var entityType = {
    name: this.entityNames.Apple,
    defaultResourceName: 'users/lookups/Apples',
    dataProperties: {
         id: {type: DT.Int32},
         image: {type: DT.String},
         name: { complexTypeName: 'Translation:#model', isNullable: false}
    }
 };


var entityType = {
    name: this.entityNames.Orange,
    defaultResourceName: 'users/lookups/Oranges',
    dataProperties: {
         id: {type: DT.Int32},
         image: {type: DT.String},
         name: { complexTypeName: 'Translation:#model', isNullable: false}
    }
 };

The meta for complex type Translation

var entityType = {
    name: 'Translation',
    isComplexType: true,
    dataProperties: {
         fr: {type: DT.String},
         en: {type: DT.String}
    }
 };

Solution

  • Your server response for apples and oranges should have the types identified in the response data. That way they can be recognized as the entities they are:

    [
      oranges:[
        {$type:model.Orange, id:1, name:'juicy'}, 
        {$type:model.Orange, id:2, name:'no seeds'}
      ],
      apples:[
        {$type:model.Apple, id:1, name:'red'}, 
        {$type:model.Apple, id:2, name:'green'}
      ]
    ]
    

    You can see more about lookups in the Breeze documentation