4

I have a backend object that i receive from the server and sent to server. I give a permission to my users to change the backend object. I would like to identify if the backend object has changed from the save point.

How can i identify it?

For example: I have a module called

app.controller('PanelCtrl', function($scope, $http) {
$scope.action = "";
$scope.selectedItem = "";
    $scope.compoundItem = [];

...

I have in the compound item array of Objects. I would like to know if something is changed whether the change occures in the primitives and whether in the compoundItem...

Aviade
  • 2,057
  • 4
  • 27
  • 49
  • I think you are looking for [scope.$watch](http://docs.angularjs.org/api/ng.$rootScope.Scope) – Satpal Jan 12 '14 at 14:29

2 Answers2

9

Edit: two scenarios 1) client check (the original post) 2) server check (extended based on the comments)

1) check on the Client

do not worry about performance of the below steps. And if you do, please read more about performance here

The answer is: angular.equals() and a similar scenario is described here: Developer Guide / forms (it is about validation). In the section Binding to form and control state, we can see the script (an extract):

function Controller($scope) {
  $scope.master = {};

  $scope.update = function(user) {
    $scope.master = angular.copy(user);
  };

  $scope.reset = function() {
    $scope.user = angular.copy($scope.master);
  };

  $scope.isUnchanged = function(user) {
    return angular.equals(user, $scope.master);
  };
}

What we can see here, is what we need. Firstly copy the source state (inside the update function). We can change isUchnaged any time, while using the angular.equals()

Also check Compare objects in Angular

NOTE: A comment to the angular.copy(). I found, that in some cases, better is to use the lo-dash .deepClone()

2) Server side solution

In case that (as Dalorzo expects) are interesting in the comparison of the server, persisted version and the client. It could make sense, if we want to check if some one else has already changed the "Entity". In some other transaction

There are in general many techniques, but the most effective is versioning. On the persistence layer, we have to introduce some value, which is changed/incremented every time the "Update" is executed. In case of MS SQL Server it would be the rowversion, see more here

Then, during any sucesfull UPDATE operation, this version will be changed, and we know that data on the client are stale... there are newer on the server.

Summary with version:

Our check could be very easy:

  • Compare the Client (current) Version property and
  • ask the server for the latest persisted.

This is a standard way I am using with NHiberante (read more here)

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • Despite of the **Dalorzo** down-vote, I would suggest, check these links. they are comming from Guide to AngularJS Documentation... – Radim Köhler Jan 12 '14 at 15:47
  • Why this is down voted ? What is the wrong ? to compare two object definitely we can use `angular.equals`. – Tasnim Reza Jan 12 '14 at 15:48
  • @Radim Köhler Tthe down-vote is because you missed " I would like to identify if the backend object has changed from the save point". I couldn't understand how this will be met from your code above. My apologies I thought you will notice it. – Dalorzo Jan 12 '14 at 15:51
  • @Dalorzo, I've updated my answer, extending it to solve your expectation that the issue is between client and server. Does this answer seem to be correct? – Radim Köhler Jan 12 '14 at 16:23
0

The first solution that comes to my mine is to use $timeout(isDifferent,xtime)

where:

isDifferent= is your custom function to get the backend object or identify when it has changed.

xtime= is and integer value in milliseconds which represents how often your function will execute.

Dalorzo
  • 19,834
  • 7
  • 55
  • 102
  • But as you described i need to compare between the current backend object and the saved one. But it is huge with many fields and objects. Can it be something more simple? I need to identify if there is only a change... in order to figure out if i have to send update request to the server or not. – Aviade Jan 12 '14 at 14:17
  • IsDifferent function can do anything it is up to use the code that you think more efficient. – Dalorzo Jan 12 '14 at 14:25