0

I have a breeze implementation where it takes a location object and displays the properties on the UI. I do a change to a few properties and try to save the changes, but breeze doesn't recognized the entity as changed. Following is my code:

    [HttpGet]
    [CustomAuthorize(Claims = "permission:CanViewLocationAttributes")]
    public Location GetLocationById(int clientId, int locationId)
    {
        //returns a single Location object

    }

Following is my client-side functionality to retrieve the entity and save the entity:

    function getLocationById(clientId, locationId) {
        var self = this;

        return EntityQuery.from('GetLocationById')
                            .withParameters({ clientId: clientId, locationId : locationId })
                            .using(self.manager)
                            .execute()
                            .then(querySucceeded, this._queryFailed);

        function querySucceeded(data) {
            if (data.results.length > 0) {
                return data.results[0];
            }
            logSuccess(localize.getLocalizedString('_RetrievedLocations_'), locations, true);
        }
    }

        function saveLocationSettings(clientId) {
        var self = this;
        var saveOptions = this.manager.saveOptions.using({ resourceName: "SaveLocationSettings",  allowConcurrentSaves: true });
        var entitiesToSave = self.manager.getChanges();
        return self.manager.saveChanges(entitiesToSave, saveOptions).then(saveSucceeded, saveFailed);
    }

my problem is that here the value of entitiesToSave is 0, even after I make changes to the fields in UI and save them.

Following is how I bind the entity to my angular model:

  function getLocationDetails() {
        clientcontext.location.getLocationById($route.current.params.clientId, $route.current.params.id)
  .then(function (data) {
      basicLocationSettings.id = data.locationId;
      basicLocationSettings.parent = data.fkParentLocationId;
      basicLocationSettings.locationType = data.locationType;
      basicLocationSettings.locationName = data.locationName;
      basicLocationSettings.locationDisplayName = data.locationDisplayName;
      basicLocationSettings.locationCode = data.locationCode;
      basicLocationSettings.isActive = data.activeStatus;
      basicLocationSettings.timeZone = data.fkTimeZoneId;
      basicLocationSettings.usesAppointments = data.usesAppointments;
      basicLocationSettings.availabilityWindowDays = data.availabilityWindowDays;
      basicLocationSettings.appointmentCutOffDays = data.appointmentCutOffDays;
      basicLocationSettings.dailySummaryEmailTime = data.dailySummaryEmailTime;
      basicLocationSettings.reminderBeforeApptEmailTime = data.reminderBeforeApptEmailTime;
      basicLocationSettings.saveLocationSettings = function () {
          clientcontext.location.saveLocationSettings($route.current.params.clientId);
      }
  });
    }

Can anyone explain what I'm doing wrong? This is my first attempt on breeze and I'm kind of stuck here.

Jeremy Danyow
  • 26,470
  • 12
  • 87
  • 133
devC
  • 1,384
  • 5
  • 32
  • 56

2 Answers2

1

It looks like you are copying the breeze location entity's property values into an pojo object variable named "basicLocationSettings". Any changes to basicLocationSettings will not be tracked by the breeze entity manager or reflected in the source breeze entity. You'll need to bind the actual breeze entity to your UI so that user data entry modifies the entity property values directly.

Jeremy Danyow
  • 26,470
  • 12
  • 87
  • 133
  • But then isn't there a way we can use some sort of a viewmodel for UI-binding? My breeze query runs through a separate repository and I use a separate controller to communicate with the UI where my pojo object is created. In that design how do I bind the breeze entity directly to the UI? – devC Jan 28 '15 at 05:00
  • That's a separate question that should be asked under the angularjs tag. I'd try to help but I don't have much experience with angular. – Jeremy Danyow Jan 28 '15 at 05:15
  • Thanks for the hint, I figured out what I was doing wrong and fixed it. I'll put the answer separately. As you suggested, it wasn't breeze that was causing the trouble, it was angular. – devC Jan 28 '15 at 05:27
1

I modified my code as follows and now the save is working:

 function getLocationById(clientId, locationId) {
        var self = this;
        var location = null;

        return EntityQuery.from('GetLocationById')
                            .withParameters({ clientId: clientId, locationId : locationId })
                            .using(self.manager)
                            .execute()
                            .then(querySucceeded, this._queryFailed);

        function querySucceeded(data) {
            if (data.results.length > 0) {
                location = data.results[0];
            }
            logSuccess(localize.getLocalizedString('_RetrievedLocations_'), locations, true);
            return location;
        }
    }

Note that I'm returning a location object, and in my controller, I bind the location object to my POJO.

        function getLocationDetails() {
        clientcontext.location.getLocationById($route.current.params.clientId, $route.current.params.id)
  .then(function (data) {
      basicLocationSettings.location = data;
      basicLocationSettings.saveLocationSettings = saveLocationSettings;
  });
    }

Now when I call saveChanges(), I pass the location object to the repository:

      function saveLocationSettings() {
        clientcontext.location.saveLocationSettings(basicLocationSettings.location);
    }
devC
  • 1,384
  • 5
  • 32
  • 56