2012-08-01 181 views
1

我currntly有一個javascript AJAX調用一個匿名函數:將變量傳遞給引用函數?

(此代碼已被削減到要領,可能有一些誤差)

function call(name){ 
    var type = services[name].type 


    $.oajax({ 
     success: function(data) { 
      fbposts=data.data 
       if (type === "grupp"){ 
        var postid=fbposts[fbpost].id.split("_"); 
        return "https://www.facebook.com/groups/"+postid[0]+"/permalink/"+postid[1]+'/' 
       } 
       else if (fbposts[fbpost].actions){ 
        return fbposts[fbpost].actions[0].link; 
       } 
      } 
     }  
    }) 
}; 

我想INSEAD使用

success: successfunction, 

和參考值的功能是這樣的:

function success(data) { 
      fbposts=data.data 
       if (type === "grupp"){ 
        var postid=fbposts[fbpost].id.split("_"); 
        return "https://www.facebook.com/groups/"+postid[0]+"/permalink/"+postid[1]+'/' 
       } 
       else if (fbposts[fbpost].actions){ 
        return fbposts[fbpost].actions[0].link; 
       } 
      } 
     }  
    }) 

此時,類型變量不再被定義。我能以某種方式解決此問題嗎?

+1

你有沒有嘗試過將類型作爲第二個變量?所以你將有函數successfunction(data,type){...},然後從成功傳遞類型作爲參數:successfunction(data,type) – Kamal 2012-08-01 10:49:24

+0

你在哪裏定義函數?如果你不需要它在'call()'之外,你可以嘗試在它內部定義它,因此它與'type'變量共享相同的範圍。 – 2012-08-01 10:50:43

回答

3

一種方法可以工作是利用上$.ajaxcontext參數 - 例如:

function successFunction(data) { 
    fbposts=data.data; 
    if (type === "grupp"){ 
     var postid=fbposts[fbpost].id.split("_"); 
     return "https://www.facebook.com/groups/"+postid[0]+"/permalink/"+postid[1]+'/' 
    } 
    else if (fbposts[fbpost].actions){ 
     return fbposts[fbpost].actions[0].link; 
    } 
} 

function call(name){ 
    var type = services[name].type; 
    $.oajax({ 
     success: successFunction, 
     context: { type: type } 
    ); 
} 

使用context原因thissuccess函數中指向任何你通過在context。所以在上面的例子中,當內successFunction我們讀到

if (type ...) 

這將意味着typethis.type,這是我們從call內保存到contexttype相同的值。

+0

太棒了!有沒有一個上下文引用的其他功能,像beforeSend,錯誤等? – Himmators 2012-08-01 11:09:30

+0

@KristofferNolgren:我還沒有測試,但文檔說上下文是針對「所有與Ajax相關的回調」。 – Jon 2012-08-01 11:16:25

+0

這是一樣的!雖然我需要指定this.type不只是在所有回調中輸入。 – Himmators 2012-08-01 11:51:38

1

是的,你可以解決通過定義success()call,所以這同樣適用範圍:

function call(name){ 
    var type = services[name].type; 
    function success(data) { 
       fbposts=data.data 
        if (type === "grupp"){ 
         var postid=fbposts[fbpost].id.split("_"); 
         return "https://www.facebook.com/groups/"+postid[0]+"/permalink/"+postid[1]+'/' 
        } 
        else if (fbposts[fbpost].actions){ 
         return fbposts[fbpost].actions[0].link; 
        } 
       } 
      }  
     }) 

    $.oajax({ 
     success: success 
    }) 
}; 

替代解決方案是保存它的外部,例如。在某個外部範圍內,在某個變量中,或者附加到其中一個DOM元素。

你也可以使用一個包裝,與正確的類型,稱爲將返回成功函數知道這種類型的這樣的:

function make_success(type){ 
    return function(data){ 
     // do what you need for success callback 
    }; 
} 
var my_success_callback = make_success('some_type'); 
0

您可以讓成功函數返回需要傳遞給ajax的函數,並將類型變量傳遞給父函數 - 就像Python裝飾器一樣。這裏有一個例子:

function call(name){ 
    var type = services[name].type; 

    $.ajax({ 
     success: success(type) 
    }) 
}; 

function success(type) { 
    return function(data) { 
     // We have access to 'type' in here! 

     fbposts=data.data; 
     if (type === "grupp"){ 
      var postid=fbposts[fbpost].id.split("_"); 
      return "https://www.facebook.com/groups/"+postid[0]+"/permalink/"+postid[1]+'/' 
     } 
     else if (fbposts[fbpost].actions){ 
      return fbposts[fbpost].actions[0].link; 
     } 
    } 
}