RHAD
RHAD

Reputation: 1357

Breeze is not storing the right value (that is set in code) when saving during entityChanged event

I'm have a Breeze, Typescript, MVC 5.2, Knockout, Entity Framework webapp. I try to update a value of an User entity when the user clicks on a row in a grid (kogrid). The value is (should be) saved in the entityChanged eventhandler, but in Fiddler I see that the property value has not changed and the entityAspect.entityState is set to Modified (!) The originalValuesMap has the old TenantId and is the only value in the map.

I subscribe to the entity changed event like this:

this.EntityManager.entityChanged.subscribe((data: breeze.EntityChangedEventArgs) => {
    if (data.entityAction == breeze.EntityAction.PropertyChange) {
                return this.EntityManager.saveChanges(<breeze.Entity[]> new Array(data.entity))
                    .fail((error) => alert("Failed. " + error));}

        });

The data arrives correctly a the eventhandler. A savechanges call is made, but the changed value (tenantId) has not changed.

The eventhandler for the rowclick is as follows:

ViewModel).OnRowClick = (tenantId: KnockoutObservable<System.IGuid>, viewModel: Imp.Scripts._TenantListViewModel) => {
        entityManager.fetchEntityByKey("User", viewModel.Settings().CurrentUser().UserId(), false)
        .then(entityKeyResult => {
            (<Imp.Classes.UserBreeze>entityKeyResult.entity).CurrentTenantId(tenantId());
            //entityManager.saveChanges(<breeze.Entity[]> new Array(entityKeyResult.entity));
         })
        .fail((error)=> alert("Error setting current tenant. " + error));});

When I disable the entityChanged subscription and enable the comment out line entityManager.saveChanges.... the entity is saved correctly. If I uncomment the line, but keep the subscription, it does not work.

How can I save the changed entity automatically after it changes?

EDIT: Workaround is to disable the entityChanged eventhandler temporarily before changing the value CurrentTenantId on the current user, save the entity manually and re-subscribe to the entityChanged event. But this solution smells.

Upvotes: 1

Views: 227

Answers (1)

Jeremy Danyow
Jeremy Danyow

Reputation: 26406

Few suggestions:

  1. Consider throttling the saves if propertychanged events are fired frequently. Knockout has a rate limiting extender for this purpose.
  2. You may also want to consider using breeze save queuing plugin so you don't need to worry about overlapping save calls as much.
  3. To troubleshoot the save issue, try adding the following code immediately before calling saveChanges:

if (data.entityAction === breeze.EntityAction.PropertyChange) { var pcArgs = <breeze.PropertyChangedEventArgs>data.args; console.log('Property Changed. PropertyName: ' + pcArgs.propertyName + '; Old Value: ' + (pcArgs.oldValue ? pcArgs.oldValue.toString() : 'null') + '; New Value: ' + (pcArgs.newValue ? pcArgs.newValue.toString() : 'null') + ';'); }

Upvotes: 1

Related Questions