Search code examples
arraysrestmergebreezecomplextype

how to add a complextype object dynamically to an array


We have created an array of complextype(Carrier field) objects. See below metadata

{           shortName : 'Person',
            namespace : 'Demo',
            autoGeneratedKeyType : breeze.AutoGeneratedKeyType.Identity,
    "dataProperties": [
        {
            "name": "carriers",
            "complexTypeName":"Carrier:#Test",
            "isScalar":false
        }]
}

The Carrier entity is defined as below:

 {
    "shortName": "Carrier",
    "namespace": "Test",
    "isComplexType": true,
    "dataProperties": [
        {
            "name": "Testing",
            "isScalar":true,
            "dataType": "String"
        }
    ]
    }

We have the following matching data for the above entities:

{
  carriers: [
             {
                Testing : 'InputBox1'
             },
             {
                Testing : 'InputBox2'
             }
            ]
}

We are trying to dynamically add the complextype object(Carrier) to the above carriers array by using the following approach:

var test = {
                "Testing" : "Test"
            };

            var result = manager.createEntity('Carrier', test);

The above code throws an exception(undefined is not a function) inside breeze.debug.js at line number 12457(see below code)

entity = entityType.createEntity(initialValues);

The exception is thrown since the complextype entity does not have 'createEntity' function in it.

What are we missing here?


Solution

  • Excellent question - Sorry I didn't have a chance to address this earlier.

    When adding a complexType object you need to use the createInstance() method instead of the createEntity.

    var thisEntityType = manager.metadataStore.getEntityType('Carrier');
    var thisEntity = thisEntityType.createInstance(initialValues);
    

    Basically you get the complexType and then create an instance of it using the values you want assigned. Keep in mind the initial values should be a hash object of course. Often I will include a helper function to do this for me like this -

    function createComplexType(entityType, constructorProperties) {
        var thisEntityType = manager.metadataStore.getEntityType(entityType);
        var thisEntity = thisEntityType.createInstance(constructorProperties);
        return thisEntity;
    }