2011-08-22 48 views
1

我想創建一個動作數組。基本上這些是知道接下來要觸發的函數的函數列表。我嘗試了很多方法,但無法讓他們可靠地工作。我做的GUI和有彈出框是回調時,他們已經完成了自己的工作(選擇文件時,編輯屬性等)Javascript:如何做鏈接列表的功能或類似?

示例流程:

任務1:打開FileBrowser(iframe的瀏覽控制),選擇時,執行任務2 任務2:從FileBrowser獲取值,打開iframe FileProperties。完成後,執行任務3 任務3:獲得FileProperties ID,在頁面控制

每個任務必須等待從以前的任務回調ID添加到。我願意接受所有想法如何做到這一點!唯一的一步是生成服務器端,所以我需要避免硬編碼!以下是我迄今的嘗試!

所有想法,感激地收到。

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(); 
      }); 
      frame.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);` 

回答

0

我認爲你正在使用.call()不正確。請參閱MDN doco on call。據我所知:

start.fn.call(start); // sets 'this' to start 

會調用該函數fn並設置其this引用start,這將默認爲已經發生的事情給你使用點符號從start調用fn。在您的函數one()中,根本沒有使用this,您使用的聲明參數似乎預計爲start。嘗試改變該行這樣的:

start.fn(start); // passes start as a parameter 

而且你one()函數中您引用callback()這似乎並沒有在任何地方定義。

0

你看過任何異步/流量控制庫嗎?例如,async提供了很多方法將功能鏈接在一起。