2011-07-14 17 views
0

これは私が持っているものです。渡すパラメータ

function populateElement(arg1) { 

    getData(arg1); 
} 

function getData(query) { 

    var url = "http://foo" + query + "&callback=processData"; 
    // do stuff 
} 

function processData(results) { 

    callbackForGetData(results); 
} 

function callbackForGetData(result) { 

    // process data 
} 

私はそうのようなpopulateElementに機能するには、2つの以上の引数渡したい:

function populateElement(arg1, arg2, arg3) { 

    getData(arg1); 
} 

とarg2のを持っているが、中ARG3が利用可能にcallbackForGetData

function callbackForGetData(result) { 

    // process data 
    // use arg2, arg3 here 
} 

どうすればよいですか?

+0

がなぜ直接のgetDataを呼び出す:

は閉鎖にデータを保存し、このような何かをお持ちですか? – Adam

+0

[複数のパラメータ/引数をjsonpコールバック関数に追加する](http://stackoverflow.com/questions/6555172/appending-multiple-parameters-arguments-to-a-jsonp-callback-function)の複製が可能です。私は、この質問に対する答えの解の概要を提示しました。 –

+0

@Adam:問題は、 'callbackForGetData'がJSONPコールバックとして呼び出され、OPが呼び出される方法をOPが制御しないということです。 –

答えて

0

あなたは、単一のparamaterとしてオブジェクトを渡すコールバックにそれらを渡すとarguments配列

function getData(result) { 
    //result === arg1 
    //arguments[0] === arg1 
    //arguments[1] === arg2 
    //arguments[2] === arg3 
    //and so on 
} 

getData(arg1, arg2, arg3); 
+0

彼のコールバック関数でarg1、arg2、arg3を得る方法は彼の質問です。 – Adam

+0

私はそれを認識しますが、私は彼が望んでいない、またはコールバックを変更することができないと仮定しています。そうでなければ、getDataにパラメータを追加して使用できます。 –

+1

これは動作しません。問題は、コールバックがJSONPを介して呼び出されることです。あなたは[このような何かをする]必要があります(http://stackoverflow.com/questions/6555172/appending-multiple-parameters-arguments-to-a-jsonp-callback-function/6555275#6555275)。 –

0

でそれらにアクセスすることができます:あなたが閉鎖を探している

function populateElement(arg1, arg2, arg3) { 

    var params = {"arg1":arg1,"arg2":arg2,"arg3",arg3}; 
    getData(params); 
} 

function getData(params) { 

    var query = params.arg1; 
    var url = "http://foo" + query + "&callback=processData"; 
    // do stuff 
} 
0

function getData(query) { 
    var url = "http://foo" + query + "&callback=processData"; 
    // do stuff 
    //simulate the callback 
    processData("some results"); 
}; 

function populateElement(arg1, arg2, arg3) { 
    //Because we define processData here, we have a closure scope that 
    // lets us see all the arguments to populateElement 
    window.processData = function(results) { 
     console.log("ProcessData called with results: " + results + 
      ", arg2: " + arg2 + ", arg3: " + arg3); 
    }; 
    getData(arg1); 
} 
//Simulate the starting call 
populateElement(4, 5, 6); 

Here's a working jsfiddle.

注これは、単一の(1)保留中のgetDataの呼び出しをサポートしています。同時に複数の保留中のgetData呼び出しをサポートするには、より複雑なスキームが必要です。それぞれには、一意のコールバッククロージャ名が必要です。これは、後続の数値シリアル番号と同じくらい単純なものです。

+0

'populateElement'の中にネストされた関数を定義する必要がありますので、' populateElement'引数にクロージャを介してアクセスします。私は明示的に 'window'に代入しています。なぜなら、それは明らかに明快で、最も混乱しないことです。それ以外の場合は、JSのデフォルトの動作が混乱することがよく知られているため、あいまいさや予期しない動作や暗黙の動作を避けることをお勧めします。私の個人的なガイドライン。 –

0

さらに引数を渡すには、callまたはapplyを使用できます。

function log(a, b, c) { console.log('A: ', a, ', b: ', b, ', c: ', c);} 

log.call(window, 'first', 'second', 'third'); 
> A: first , b: second , c: third 

log.apply(window, ['first', 'second', 'third']) 
> A: first , b: second , c: third 

しかしピーターは示唆しているとして、あなたはここで起こっていくつかの非同期のものを持っていて、閉鎖にこれを保存する代わりに、追加の引数を渡します。

function processData(results) { 
    // transform data 
    myModule.callbackForGetData(results); 
} 

window.myModule = { 

    populateElement: function(arg1, arg2, arg3) { 

    this.data = arguments; 
    this.getData(arg1); 
    }, 

    getData: function(query) { 

    var script = document.createElement('script'); 
    script.src = 'http://jsfiddle.net/echo/jsonp/?query=' + query + '&callback=processData'; 
    document.getElementsByTagName('head')[0].appendChild(script) 
    }, 

    callbackForGetData: function(result) { 

     // process data 
     console.log('Args: ', this.data, ', remote result: ', result); 
    } 
} 

// test it 
myModule.populateElement(1, 2, 3) 
> Args: [1, 2, 3] , remote result: Object {query: "1"}