Search code examples
breeze

In Breeze (entityframework, knockout) how can I access the original Entity after a query has completed


I am wanting to pass down some pseudo information about an Entity to the client and have that available with my entity. From what I can tell only mapped properties of the entity are available after query execution. Is there a hook I am missing or some other way I can get at the raw results of the API call that fetched a given entity from the server?

e.g:

server: 

class Patient
{
    [NotMapped]
    public string Name => First + " " + Last;
    public string First {get;set;}
    public string Last {get;set;}
}

client:

this._executeQuery(breeze.EntityQuery.from('api/Patient/1'))                       
  .then(function (data) {
    var data = data[0];
    // data = PatientCtor with first and last properties only
});

I would like data.name to be available in the client. It is sent down with the query to the client but does not come out on the other end of the query.


Solution

  • You can add the properties to the client-side definition of the entity, and Breeze will populate them for you. You can do this by registering a custom constructor function for the entity:

    function Patient() {
        this.name = "";
    }
    var em = new breeze.EntityManager();
    em.metadataStore.registerEntityTypeCtor("Patient", Patient);
    

    Note that you need to do this before your first query.

    Then, when breeze creates Patient entities as a result of a query, it will populate the unmapped name property.

    See the Breeze documentation on Extending Entities.