Search code examples
breeze

breezejs how to using as datasource


i'm using breezejs for some time now, and i'm happy about it and it's rich functionality , but the problem with breezejs is that i cant use it as datasource almost for anything.

there is no grid that you could show data and not losing functionality, and you cant use your array of entities as normal array. (so you cant use it as datasource for dropdown ...)

so for showing data in UI i end up converting data to normal array and losing Breeze functionality (like track change) and before save converting them back.

some code like this one for converting to normal array:

  if(data.length>0)
{
       var names = Object.getOwnPropertyNames(data[0]._backingStore);
       var columns: string[] = [];
       for (var i = 0; i < names.length; i++)
       {
           columns.push(names[i]); //Getting columns name
       }
       var newArray = [];
       data.forEach((item, index, array) => {
            newArray.push(item._backingStore);
       });
}

my question is how do you show your data in UI using breezejs properly? (i'm using angualr (hottowel))


Solution

  • Assuming you're trying to solve issues like this:

    1. The grid doesn't like the entityType and entityAspect properties on Breeze entities.
    2. This grid doesn't know how to handle Knockout style "properties" that are functions.
    3. Creating a POCO object using the Breeze entity's property values disconnects you from the change tracking goodness.

    You could try creating your POCO object using Object.defineProperty, using the Knockout observable as the property's getter and setter functions. Here's a simple example:

    Typescript + Knockout:

    class PocoBreezeEntity {
        constructor(private entity: breeze.Entity) {
            entity.entityType.dataProperties.forEach(
                dataProperty => {
                    Object.defineProperty(
                        this,
                        dataProperty.name,
                        {
                            get: entity[dataProperty.name],
                            set: entity[dataProperty.name],
                            enumerable: true,
                            configurable: true
                        });
                });
        }
    }
    

    Typescript + Angular:

    class PocoBreezeEntity {
        constructor(private entity: breeze.Entity) {
            entity.entityType.dataProperties.forEach(
                dataProperty => {
                    Object.defineProperty(
                        this,
                        dataProperty.name,
                        {
                            get: function() { return entity[dataProperty.name]; },
                            set: function(newValue) { entity[dataProperty.name] = newValue; },
                            enumerable: true,
                            configurable: true
                        });
                });
        }
    }
    

    With this kind of approach you have the benefits of POCO entities without losing the Breeze change tracking.