Currently I have an Angular form like
<form>
Name: <input name="name" [(ngModel)]="data.name"><br>
Age: <input name="age" [(ngModel)]="data.age"><br>
City: <input name="city" [(ngModel)]="data.city"><br>
<button (click)='update()'>
</form>
Upon update I'd like to be able to have an object containing just changed fields. Just for quick and dirty solution I achieved my need by using Proxy which looks more or less like below (with assigning component's data to be changeTracker.proxy):
class ChangeTracker {
proxy;
changed;
constructor(initialTarget = {}) {
this.changed = {};
this.proxy = new Proxy(initialTarget, {
get(target, key) {
return target[key];
},
set(target, key, val) {
target[key] = val;
this.changed[key] = val;
return true;
}
});
}
}
So eventually I can do i.e. REST PUT
http.put('/some/api/type', { objectId, ...changeTracker.changed });
I was not able to find any SO answer or external tutorial addressing directly the problem of getting set of changes from the form. Wonder what's the Angular way of achieving this - the best would be to avoid unnecessary boilerplate yet looking for any reasonable solution.