Search code examples
javascriptbreeze

Breeze JS - Extending model from object graph


Currently I am able to extend the breeze model entity type of my Product entity using the following code:

    function registerProduct(metadataStore) {


        function Product(){}


        // description property
        Object.defineProperty(Product.prototype,'description', {
            // TODO: handle selected globalization language
            get: function () {
                return this.descripFr;
            }
        })

        metadataStore.registerEntityTypeCtor('Product',Product);
    }

The issue I am having is using a property from the entity graph (in this case codeligne) in an extended property like so:

      Object.defineProperty(Product.prototype,'name', {
            get: function () {
                var codeligne = this.codeligne;
                var name = codeligne.ligne + '-' + this.codeprod;
                return name;
            }
        })

This throws an undefined exception for codeligne.ligne. If I directly use the codeligne.ligne in an ng-repeat then the property is displayed properly so Breeze seems aware of it.

Any suggestions on how to use the codeligne graphed object when extending the model?


Solution

  • It is possible that the entity represented by the "ligne" navigation property isn't loaded yet. You should check that it contains a value before referencing its properties.

      Object.defineProperty(Product.prototype, 'name', {
            get: function () {
                var codeligne = this.codeligne;
    
                if (!codeligne) {
                    return null;
                }
    
                var name = codeligne.ligne + '-' + this.codeprod;
                return name;
            }
        })