When considering the question at hand, there are two potential scenarios to address.
If a user fills out the fields and clicks on the SAVE button, a PATCH request will be sent to save/update the values.
As the user makes changes to the values, a request must be triggered.
Regarding the first scenario:
To keep track of any changes made, it is suggested to maintain a hashmap. Utilize the ng-modal
directive for all user input fields.
Let's take, for example, two input fields: username
and password
.
<input type="text" ng-modal="username" />
<input type="password" ng-modal="password" />
In your controller:
$scope.userInfo = {
user: $scope.username,
pass: $scope.password
};
Any changes will automatically reflect in your views and models through two-way binding.
Now, you can compare the old and new values to determine which ones to send as needed.
For the second scenario:
Utilize Angular's watchCollection
function.
scope.$watchCollection('[username, password]', function () {
// initiate a request
});
EDIT
It is possible to use a debounce method for the second approach.
// Returns a function that, when continuously invoked, won't trigger repeatedly unless specified time has passed.
_.debounce = function(func, wait, immediate) {
var timeout, args, context, timestamp, result;
var later = function() {
var last = _.now() - timestamp;
if (last < wait && last >= 0) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) {
result = func.apply(context, args);
if (!timeout) context = args = null;
}
}
};
return function() {
context = this;
args = arguments;
timestamp = _.now();
var callNow = immediate && !timeout;
if (!timeout) timeout = setTimeout(later, wait);
if (callNow) {
result = func.apply(context, args);
context = args = null;
}
return result;
};
};
Source: https://github.com/jashkenas/underscore/blob/master/underscore.js