2012-12-10 47 views
1

問題:的JavaScript生成功能

我有以下腳本,基本上,我是從一個Ajax調用獲取數據,將數據傳遞給函數,存儲從數據的ID在全球可變的,所以全局變量可以在從jQuery的$.getScript()檢索不同的腳本中使用:

腳本(script1.js):

該位只是獲取(未示出)通過AJAX數據位,但它在widget_data.d它應該基於所述數據的在widget_data.d長度運行getWidgetContent()功能,在這種情況下3次迭代:

window.global_widget_id = ""; 

for (j = 0; j <= widget_data.d.length - 1; j++) { 

    getWidgetContent(widget_data.d[j]); 

} 

這是函數上面運行的循環:

function getWidgetContent(widget) { 

    if(widget.script!=null){ 

     window.global_widget_id = widget.widget_id; 

     $.getScript("js/script2.js", function() { 

      alert("direct title variable in script1.js: " + widget.title); 
      alert("global variable in script1.js: " + window.global_widget_id); 
      alert("direct variable in script1.js: " + widget.widget_id); 

      $(".widget_header_title_" + widget.widget_id).append(widget.title); 

     }); 


    } 

} 

該腳本(script2.js):

這是上面的函數也傳遞全局變量的腳本,它應該通過基於全局存儲的id的ajax獲取數據。

var my_widget_id = window.global_widget_id; 

alert("global variable in script2.js " + window.global_widget_id); 
alert("direct variable in script2.js: " + my_widget_id); 

// then do some more ajax stuff with global_widget_id before repeating the loop again. 

實際結果:

global variable in script2.js: 66 
direct variable in script2.js: 66 
direct title variable in script1.js: title for 57 goes here 
global variable in script1.js 66 
direct variable in script1.js 57 

global variable in script2.js: 66 
direct variable in script2.js: 66 
direct title variable in script1.js: title for 65 goes here 
global variable in script1.js 66 
direct variable in script1.js 65 

global variable in script2.js: 66 
direct variable in script2.js: 66 
direct title variable in script1.js: title for 66 goes here 
global variable in script1.js 66 
direct variable in script1.js: 66 

預期結果:

global variable in script2.js: 57 
direct variable in script2.js: 57 
direct title variable in script1.js: title for 57 goes here 
global variable in script1.js 57 
direct variable in script1.js 57 

global variable in script2.js: 65 
direct variable in script2.js: 65 
direct title variable in script1.js: title for 65 goes here 
global variable in script1.js 65 
direct variable in script1.js 65 

global variable in script2.js: 66 
direct variable in script2.js: 66 
direct title variable in script1.js: title for 66 goes here 
global variable in script1.js 66 
direct variable in script1.js: 66 

我曾嘗試:

基於this website,我可能創建一個generator function。這裏是一個模板:

(function(variable) { 
    return function() { 
    // do something with variable 
    } 
})(value); 

我已經使用這個嘗試,但沒有任何反應,沒有錯誤,沒有警報,什麼都沒有,即:

for (j = 0; j <= widget_data.d.length - 1; j++) { 

    var the_data = widget_data.d[j]; 

    (function(the_data) { 
     return function() { 
     getWidgetContent(the_data ); 
     } 
    })(the_data); 

} 

問題:

爲什麼發電機功能不工作?

回答

0

你在做什麼,正在返回一個函數,調用你的getWidgetContent

(function(the_data) { 
    return function() { // you will return a function, it will not get called. 
    getWidgetContent(the_data ); 
    } 
})(the_data); // the (the_data) means you call the first function with the_data as parameter. 

如果你想打電話給getWidgetContent,你需要直接調用它。

(function(the_data) { 
    return getWidgetContent(the_data ); 
})(the_data); 
// this will call your anonymous function with the_data as parameter, giving closure. 

但從我所看到的情況來看,這不是您唯一的擔憂。打印script1.js輸出的成功函數在script2.js運行後調用,可能在加載循環之後調用。

編輯: 的時候script2.js被裝載,就已經有57,65,66,但因爲你覆蓋它,script2.js只能看到最後一個值,66設置全局變量的3倍。你的循環比腳本下載要快得多,它是異步的。

我認爲你應該做的就是把你的script2代碼放入一個函數中,並且使用widget作爲參數從script1成功回調中調用它。

function getWidgetContent(widget) { 

    if(widget.script!=null){ 

    window.global_widget_id = widget.widget_id; 

    $.getScript("js/script2.js", function(inner_widget) { 
     return function() { 
      // this is the function that will get called as the callback. 
      alert("direct title variable in script1.js: " + inner_widget.title); 
      alert("global variable in script1.js: " + window.global_widget_id); 
      alert("direct variable in script1.js: " + inner_widget.widget_id); 
      // inner_widget is the parameter, so it is in a closure 
      $(".widget_header_title_" + inner_widget.widget_id).append(inner_widget.title); 
      //make your call to script2.js if you want. 
      script2.run(inner_widget); 
     } 
    }(widget)); 
    //we call the generator function with the widget to get closure. 
    } 
} 
+0

我沒有提到的是,我將有幾百個小腳本,我會想用$ .getScript僅在需要時根據用戶的選擇調用。如果我從一開始就調用所有腳本,那隻會減慢速度並增加下載時間。 – oshirowanen

0

心不是IST剛:

for (j = 0; j <= widget_data.d.length - 1; j++) { 

    var the_data = widget_data.d[j]; 

    (function(the_data) { 
    getWidgetContent(the_data); 
    })(the_data); 

} 

否則我不能看到返回的函數被調用