I am trying to create an actions array. Basically these are a list of functions that know the function to be fired next. I've tried lots of ways but can't get them to work reliably. I am doing gui and have pop up frames that callback when they have completed their work (Select file, edit properties etc.)
Example flow:
Task 1: open FileBrowser (iframe browsing control), when selected, execute Task 2 Task 2: get value from FileBrowser, open iframe FileProperties. when complete execute Task 3 Task 3: get FileProperties id, add id to in page control
Each task has to wait for a callback from the previous task. I am open to all ideas how to do this! The only thing is the step are generated server side so I need to avoid hard coding! Below is my attempt so far!
All ideas, gratefully received.
FruitBat
/* action array */
var actions = new Array();
var data = {};
var op = function(fn, next, data) {
this.op = this;
this.fn = fn;
this.next = next;
this.data = data;
};
function one(myop) {
var op1 = myop;
var url = 'poptarget.aspx?cid=21&data=' + escape('[Id]=[2],[cid]=[3]');
var frame = $().popFrame('UniqueId', { caption: 'Page Item', url: url, width: 'auto', modal: true });
frame.bindEvent("cancelled saved", function(e, d) {
console.log('other');
callback();
});
开发者_如何学Cframe.bindEvent("close", function(e, d) {
console.log('close');
op1.next.fn.call(op1.next);
});
};
function two(myop) {
console.log('woohoo');
};
actions.push(new op(one, two, null));
actions.push(new op(two, null, null));
var start = actions[0];
start.fn.call(start);
console.log(data);`
I think you are using .call()
incorrectly. See the MDN doco on call. As I understand it:
start.fn.call(start); // sets 'this' to start
will call the function fn
and set its this
to reference start
, which would've happened by default given that you call fn
using dot notation from start
. Within your function one()
you are not using this
at all, you are using a declared parameter that you seem to expect to be start
. Try changing that line to this:
start.fn(start); // passes start as a parameter
Also within your one()
function you reference callback()
which doesn't seem to be defined anywhere.
Have you looked at any async/flow control libraries? For example, async provides a pretty comprehensive number of ways to chain functions together.
精彩评论