[fix] fix ajax call to update doc not replace (#5204)
This commit is contained in:
parent
1537e566ea
commit
ba5ec5bcc8
1 changed files with 35 additions and 1 deletions
|
|
@ -20,7 +20,12 @@ $.extend(frappe.model, {
|
|||
for(var i=0, l=r.docs.length; i<l; i++) {
|
||||
var d = r.docs[i];
|
||||
|
||||
frappe.model.add_to_locals(d);
|
||||
if (locals[d.doctype] && locals[d.doctype][d.name]) {
|
||||
// update values
|
||||
frappe.model.update_in_locals(d);
|
||||
} else {
|
||||
frappe.model.add_to_locals(d);
|
||||
}
|
||||
|
||||
d.__last_sync_on = new Date();
|
||||
|
||||
|
|
@ -97,6 +102,35 @@ $.extend(frappe.model, {
|
|||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
update_in_locals: function(d) {
|
||||
// update values in the existing local doc instead of replacing
|
||||
let local_doc = locals[d.doctype][d.name];
|
||||
for (let fieldname in d) {
|
||||
if (local_doc[fieldname] instanceof Array) {
|
||||
// table
|
||||
if (!(d[fieldname] instanceof Array)) {
|
||||
d[fieldname] = [];
|
||||
}
|
||||
// child table, override each row and append new rows if required
|
||||
for (let i=0; i < d[fieldname].length; i++ ) {
|
||||
if (local_doc[fieldname][i]) {
|
||||
// row exists, just copy the values
|
||||
Object.assign(local_doc[fieldname][i], d[fieldname][i]);
|
||||
} else {
|
||||
local_doc[fieldname].push(d[fieldname][i]);
|
||||
}
|
||||
}
|
||||
|
||||
// remove extra rows
|
||||
if (local_doc[fieldname].length > d[fieldname].length) {
|
||||
local_doc[fieldname].length = d[fieldname].length;
|
||||
}
|
||||
} else {
|
||||
// literal
|
||||
local_doc[fieldname] = d[fieldname];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue