Julián Yuste
Julián Yuste

Reputation: 1472

Bug when deleting unsaved entity

I think I've found a bug when you add a child entity to another entity and then mark the child entity as deleted (Without saving changes previously).

Here is the test:

test("delete unsaved entity", 1, function () {
    var realEm = newEm();
    //ok(realEm.hasChanges() === false, "The entity manager must not have changes");
    var query = EntityQuery.from("Customers")
        .where("CustomerID", "==", "729de505-ea6d-4cdf-89f6-0360ad37bde7")
        .expand("Orders");
    stop();
    realEm.executeQuery(query)
        .then(function (data) {
            var cust = ko.observable(data.results[0]);
            var newOrder = realEm.createEntity("Order", {}, breeze.EntityState.Detached);
            cust().Orders.push(newOrder);
            //ok(newOrder.entityAspect.entityState.isAdded() === true, "The entity is Added");
            newOrder.entityAspect.setDeleted();
            //ok(realEm.hasChanges() === true, "The entity manager must have changes? Not clear to me but it's true");
            realEm.saveChanges();
            ok(realEm.hasChanges() === false, "The entity manager must not have changes");
        })
    .fin(start);
});

Upvotes: 2

Views: 247

Answers (1)

Jay Traband
Jay Traband

Reputation: 17052

As of breeze v1.0.0 this bug is now fixed. Sorry for the delay

Old post below

Thanks for the repro, it really helps. There is a bug here and it will be fixed in the next release probably later today or early tomorrow.

Just to be clear the bug is that the 'hasChanges' call after the 'setDeleted' call should return false, but currently returns true. The reason is that deleting an 'added' record simply detaches the entity from the entityManager; hence reverting the entityManager to the state it was in before the 'add'. The detach does occur, but the hasChanges function is broken in this case.

Two other issues though.

First, you can replace this line

 var cust = ko.observable(data.results[0]);

with this

 var cust = data.results[0];

because Breeze will automatically create ko observables out of any entity returned from a query.

and second your call to saveChanges

  realEm.saveChanges();
  ok(realEm.hasChanges() === false);

needs to be converted to a promise, because saveChanges is asynchonous.

  realEm.saveChanges().then(function(r) { 
      ok(realEm.hasChanges() === false);
  }

Upvotes: 1

Related Questions