Search code examples
angularjsbreeze

Updating entity associations using ngGrid and breeze.js


I am building a web application that combines AngularJS, ngGrid, and breeze.js.

I have an entity association in my ORM where the parent entity (call it 'Foo') has an association to one 'Bar' entity.

breeze.js queries and loads the 'Foo' entities and their 'Bar' associations fine.

In my ngGrid I want to let the user change the association by providing a drop down input showing the name of each Bar entity locally cached. In my ngGrid's columnDefs property I have this for the target field's definition:

columnDefs: [
           {
               field: 'Bar', displayName: 'Bar', enableCellEdit: true,
               cellTemplate: '<select ng-class="\'colt\' + col.index" ng-input="COL_FIELD" ng-model="data[row.rowIndex].Bar" ng-options="bar as bar.name for bar in bars"  />'
           }]

In my angular controller I have 'bars' defined in my scope as the array of breeze entities queried from local cache and represented in the ng-options

For ng-model, "data" is a scoped array for each of the Foo entities and is also the source for the ngGrid data.

The drop down loads okay. I can see the current value for Foo as the selected option and I get a drop down of each selectable Bar name to change Foo's association to a new Bar.

The problem is when I select a new Bar from the drop down the breeze.js framework attempts to add the selected bar to the breeze cache and treat it as new before it changes the association when in fact bar is an already existing entity in the cache. The error thrown is "An entity with this key is already in the cache"

This happens before any attempt to capture the change using ng-change or handle breeze changes with the EntityManager entityChanged event.

I'm unable to figure out why breeze.js is treating the selected bar in the drop down list as a new entity rather than just change the association of Foo and how to handle that?


Solution

  • By cloning the breeze entity data into a separate object and into a new collection I was able to work around this issue.