2013-05-31 22 views
0

我想檢索用戶創建的所有監視列表的列表,以顯示在我的自定義MVC4應用程序中。我如何獲得jquery中用戶的stocktwit監視列表

我可以對用戶進行身份驗證,並且如果我知道watchlistid,也可以獲得關注列表Feed。在我們的應用程序中,我們希望用戶選擇他現有的關注列表來顯示供稿。

回答

0

知道從什麼地方開始Stocktwits,我發現這個文件難以置信的幫助:

http://stocktwits.com/developers/docs/signin

一旦你得到了大流,取代AJAX調用更多的東西像這樣

$.ajax({ 
     url: "https://api.stocktwits.com/api/2/watchlists.json?callback=?", 
     dataType: 'jsonp', 
     timeout: 5000, 
     data:{ access_token: token}, 
     success: function(data) { 
      if (data) { 
      callback(data); 
     }}, 
     error: function(error){ 
     //handle error 
     } 

現在我們有一個監視列表。在這之後,你要寫一個回調函數來使用剛剛得到的數據:

var getWatchlist = function(data){ 
    if(!data) return undefined;  //Sanity checks are always good! 
    if(data.response == undefined) return undefined; 

     var postInfo = []; //A list to put our results on! 

    if(data.response.status == "401") { //Represents a bad token. Handle and return 
    return; 
    } 
    var watchlistData = data.watchlists; 
    for (var i=0; i<watchlistData.length; i++) 
    { 
    var watchlistId = watchlistData[i].id; 

    var tempInfo = {watchlistId}; 
    postInfo.push(tempInfo); 
    } 
return postInfo; //All of your watchlists! 
} 

從這裏開始,使用這裏描述的類似的功能:

http://stocktwits.com/developers/docs/api#watchlists-show-docs

$.ajax({   //Note you have to put in the watchlist_id 
     url: "https://api.stocktwits.com/api/2/watchlists/show/"+watchlist_id+".json?callback=?", 
     dataType: 'jsonp', 
     timeout: 5000, 
     data:{ access_token: token}, 
     success: function(data) { 
      if (data) { 
      callback(data); 
     }}, 
     error: function(error){ 
     //handle error 
     } 

照此再次用不同的回調排序:

var getWatchlistData = function(data){ 
    if(!data) return undefined; //Sanity is still important, for some.... 
    if(data.response == undefined) return undefined; 

    var postInfo = []; //A list to put our results on! 

    if(data.response.status == "401") { //Represents a bad token. Handle and return 
    return; 
    } 
    var watchlistSymbolsData = data.watchlists.symbols; 
    for (var i=0; i<watchlistSymbolsData.length; i++) 
    { 
    var watchlistTicker = watchlistData[i].symbol; 
      var watchlistId = watchlistData[i].id; 
    var tempInfo = {watchlistTicker, watchlistId}; //You'll probably want some more data here 
    postInfo.push(tempInfo); 
    } 
return postInfo; //All of your tickers with corresponding ids! 
} 

只需創建一個函數來調用AJAX,將其中一個獲取函數作爲回調函數,然後您就可以很好地顯示任何您想要的內容。