From a17d7520c44ec1ed3b0fc836172c7f48bb4acab7 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Thu, 21 May 2015 12:43:19 +0530 Subject: [PATCH 1/2] after ajax --- frappe/public/js/frappe/request.js | 38 +++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/frappe/public/js/frappe/request.js b/frappe/public/js/frappe/request.js index 654eb9d84c..0be0ca02a9 100644 --- a/frappe/public/js/frappe/request.js +++ b/frappe/public/js/frappe/request.js @@ -5,6 +5,8 @@ frappe.provide('frappe.request'); frappe.request.url = '/'; +frappe.request.ajax_count = 0; +frappe.request.waiting_for_ajax = []; // generic server call (call page, object) frappe.call = function(opts) { @@ -110,7 +112,9 @@ frappe.request.call = function(opts) { data = JSON.parse(data.responseText); } frappe.request.cleanup(opts, data); - if(opts.always) opts.always(data); + if(opts.always) { + opts.always(data); + } }) .done(function(data, textStatus, xhr) { var status_code_handler = statusCode[xhr.statusCode().status]; @@ -131,6 +135,8 @@ frappe.request.call = function(opts) { // call execute serverside request frappe.request.prepare = function(opts) { + frappe.request.ajax_count++; + $("body").attr("data-ajax-state", "triggered"); // btn indicator @@ -200,22 +206,16 @@ frappe.request.cleanup = function(opts, r) { // debug messages if(r._debug_messages) { - console.log("-") - console.log("-") - console.log("-") if(opts.args) { - console.log("<<<< arguments "); + console.log("======== arguments ========"); console.log(opts.args); - console.log(">>>>") + console.log("========") } $.each(JSON.parse(r._debug_messages), function(i, v) { console.log(v); }); - console.log("<<<< response"); + console.log("======== response ========"); delete r._debug_messages; console.log(r); - console.log(">>>>") - console.log("-") - console.log("-") - console.log("-") + console.log("========"); } if(r.docs || r.docinfo) { @@ -226,6 +226,22 @@ frappe.request.cleanup = function(opts, r) { } frappe.last_response = r; + + frappe.request.ajax_count--; + if(!frappe.request.ajax_count) { + $.each(frappe.request.waiting_for_ajax || [], function(i, fn) { + fn(); + }); + frappe.request.waiting_for_ajax = []; + } +} + +frappe.after_ajax = function(fn) { + if(frappe.request.ajax_count) { + frappe.request.waiting_for_ajax.push(fn); + } else { + fn(); + } } frappe.request.report_error = function(xhr, request_opts) { From 2b2239955c2504bba9b03831a79d8b39abdfe1d6 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Thu, 21 May 2015 12:43:53 +0530 Subject: [PATCH 2/2] set qty after completion of item code ajax --- frappe/public/js/frappe/form/link_selector.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/frappe/public/js/frappe/form/link_selector.js b/frappe/public/js/frappe/form/link_selector.js index 282d9c9716..86f975a29a 100644 --- a/frappe/public/js/frappe/form/link_selector.js +++ b/frappe/public/js/frappe/form/link_selector.js @@ -115,7 +115,6 @@ frappe.ui.form.LinkSelector = Class.extend({ if(this.qty_fieldname) { frappe.prompt({fieldname:"qty", fieldtype:"Float", label:"Qty", "default": 1, reqd: 1}, function(data) { - console.log(data); $.each(me.target.frm.doc[me.target.df.fieldname] || [], function(i, d) { if(d[me.fieldname]===value) { frappe.model.set_value(d.doctype, d.name, me.qty_fieldname, data.qty); @@ -126,9 +125,13 @@ frappe.ui.form.LinkSelector = Class.extend({ }); if(!updated) { var d = me.target.add_new_row(); - frappe.model.set_value(d.doctype, d.name, me.qty_fieldname, data.qty); frappe.model.set_value(d.doctype, d.name, me.fieldname, value); - show_alert(__("Added {0} ({1})", [value, data.qty])); + frappe.after_ajax(function() { + setTimeout(function() { + frappe.model.set_value(d.doctype, d.name, me.qty_fieldname, data.qty); + show_alert(__("Added {0} ({1})", [value, data.qty])); + }, 100); + }); } }, __("Set Quantity"), __("Set")); } else {