ich sehe, dass c nicht von abhängt b Ergebnis, und b hängt nicht von ein Ergebnis ab.
den GRASP Prinzipien verfolgt (http://en.wikipedia.org/wiki/GRASP_(object-oriented_design), ein muss wissen nicht b und b nicht c wissen müssen.
Wenn wir programmieren, um die GRASP Grundsätze oder Richtlinien zu erinnern sehr wichtig.
High Cohesion und Low Kopplung bedeuten, dass unser Code besser sein wird, mehr wiederverwendbar und leichter zu pflegen.
Die Hauptfunktion, die a, b und c kennt, muss die verketteten Aufrufe erstellen.
würden die Funktionen sein:
// Array with params for a function (a function is the first link in chain)
var data = [{param1 : 1235, param2: 3214}, {param1 : 5432, param2: 9876}];
// Array with calls to each fixed sequence a, b, and c. We iterate over data array
var arrayFunctions = [];
$.each(data, function(i,obj) {
arrayFunctions.push(
function() {
console.log("Params in data with index " + i + ":");
// We define the fixed sequence: a with params, b without params and c without params
return $.iterativeWhen(
function() {
return a(obj.param1, obj.param2);
},
b,
c
);
}
)
});
// Start the global process
$.iterativeWhen.apply($, arrayFunctions)
.done(function() {
console.log ("----------------");
console.log ("> Global Success");
})
.fail(function() {
console.log ("--------------");
console.log ("> Global Fail");
});
$ .iterativeWhen existiert nicht in jQuery, so habe ich es gebaut:
function a(param1, param2) {
var deferred = $.Deferred();
console.log(" function a: begin. Params " + param1 + " and " + param2);
mockPost("a_url").done(function() {
console.log(" function a: end ok. Params " + param1 + " and " + param2);
deferred.resolve();
}).fail(function() {
console.log(" function a: end fail. Params " + param1 + " and " + param2);
deferred.reject();
});
return deferred.promise();
}
function b() {
var deferred = $.Deferred();
console.log(" function b: begin");
mockPost("b_url").done(function() {
console.log(" function b: end ok.");
deferred.resolve();
}).fail(function() {
console.log(" function b: end fail.");
deferred.reject();
});
return deferred.promise();
}
function c() {
// We suppose that c function calls to post function and anything more
return mockPost("c_url");
}
Die Hauptfunktion sein würde. Es funktioniert mit jQuery 1.8 und späteren Versionen.
$.iterativeWhen = function() {
var deferred = $.Deferred();
var promise = deferred.promise();
$.each(arguments, function(i, obj) {
promise = promise.then(function() {
return obj();
});
});
deferred.resolve();
return promise;
};
Die mockPost Funktion simuliert einen Aufruf $ .post mit einer Erfolgswahrscheinlichkeit:
function mockPost(url) {
var deferred = $.Deferred();
setTimeout(function() {
if (Math.random() <= 0.9) {
console.log(" request url: " + url + "... ok");
deferred.resolve();
} else {
console.log(" request url: " + url + "... fail");
deferred.reject();
}
}, 1000);
return deferred.promise();
}
Die Protokollausgabe ist:
Params in data with index 0:
function a: begin. Params 1235 and 3214
request url: a_url... ok
function a: end ok. Params 1235 and 3214
function b: begin
request url: b_url... ok
function b: end ok.
request url: c_url... ok
Params in data with index 1:
function a: begin. Params 5432 and 9876
request url: a_url... ok
function a: end ok. Params 5432 and 9876
function b: begin
request url: b_url... ok
function b: end ok.
request url: c_url... ok
----------------
> Global Success
jsFiddle hier: http://jsfiddle.net/E2tp3/
nichts ist falsch mit ur Struktur .. u kann genauen abt des Code geben .. – Sarath
ja, es ist nichts falsch mit ihm, aber wenn Sie lesen die Ausführung entlang, Code, wenn Schleife ist aa, bb, cc anstelle von abc, a bc –
also hw ur bleib dran was zuerst heißt .. jetzt stellst du konsole nur auf fail .. – Sarath