Search code examples
breeze

entity gets detached somehow between ui-router resolve and the controller


I'm trying to reject changes on a newly added entity (expecting breeze to set entityState to detached). Most times it works, but once in a while (mostly after I have already performed an entity create and reject), the rejectChanges call will fail because there is a null reference to the entityAspect.entityManager property.

I am getting my entity instance through "resolve" of ui-router on abstract parent state. Then in the abstract parent controller, I assign that instance to $scope. From then on I deal with $scope.myEntity instead of the resolved variable in my constructor. When I call entity This is my resolve function:

 resolve:{
    myEntity:function(sharedEntityManager,$stateParams, $q){
      return  $q.when(sharedEntityManager.getMyEntity($stateParams.entityId))
        .then(function(myEntity){
          if(!myEntity && myEntity.entityAspect.entityState.isDetached()){
            $q.reject();
          }
          else
          {
            return myEntity;
          }
        });
    }
  }

Then in my parent abstract controller, I call this:

$scope.myEntity = myEntity;

The error I get showing null entityManager...

TypeError: Cannot read property 'isRejectingChanges' of null
at __using (breeze.debug.js:449)
at EntityAspect.proto.rejectChanges (breeze.debug.js:3687)

The error seems to occur only after I have successfully done 1 cycle of creating entity, rejecting that entity. Then when I create another new entity, that new entity somehow goes from "Added" and having an entityManager, to "Detached" and null entityManager. I confirmed that in the resolve function, entityState is "Added", but in the controller constructor, the injected entity from ui-router is now "Detached".

Anyone familiar with this behavior and is there a work around?


Solution

  • Are you sure about the test in the then function? It seems off to me. Do you really mean if(!myEntity && ... or should it be if(!myEntity || ...)