Suppose I able to get ($.ajax
), process (process_*
) and save (store_* =
) data A
and B
independently and already have API:
var store_A;
function A() {
$.ajax({url: "/getA"}).done(function(data) {
store_A = process_A(data);
}); }
var store_B;
function B() {
$.ajax({url: "/getB"}).done(function(data) {
store_B = process_B(data);
}); }
I have a function C()
that able to combine store_A
and store_B
into something:
var store_C;
function C() {
store_C = process_C(store_A, store_B);
}
Suppose that A()
, B()
and C()
is public API and all other stuff is internals and actually complex code (so for example I can't chain both $.ajax
directly).
I want to refactor above code to new API by using jQuery Deferred API so I can request any case:
case1: after(A).do(C)
case1: after(B).do(C)
case2: afterParallelJobsFinished(A, B).do(C)
and be sure that store_A
or store_B
updated as requested and store_C
updates only after update of one or both of A/B as requiested.
Imaging that we have webapp where user manage set of income and expenses.
On page loading we get incomes and expenses from different data-sources (that is $.ajax
) in parallel, render view and store data (usually that are intermixed process_*
/ store_* =
) and show total = SUM(incomes) - SUM(expenses)
when all data arrived.
That is case2
.
When user edit expenses and request partial page update we in case1
because we need only load/render/store expanses to get correct total = SUM(incomes) - SUM(expenses)
.