2012-12-08 31 views
1

我不熟悉sencha touch 2.我無法設置我的qrCodeHtml變量並在JsonP請求之外使用它。我知道所有這些代碼的工作原理除了能夠設置qrCodeHtml變量。請幫我做到這一點:Sencha Touch:如何在JsonP請求中設置變量

onMyProfileCommand: function() { 
    var api_username = "o_xxxxx"; 
    var api_key = "R_xxxxxxxxxx"; 
    var long_url = "http://google.com"; 
    var qrCodeHtml = ''; 

    Ext.data.JsonP.request({ 
     url: 'https://api-ssl.bitly.com/v3/shorten', 
     callbackKey: 'callback', 
     params: { 
      login: api_username, 
      apiKey: api_key, 
      longUrl: long_url 
     }, 
     success: function (result, request) { 
      var shortUrl = result.data.url; 
      qrCodeHtml = '<div style="font-size:15px; margin-bottom:5px;">Friends can scan this to <a href="' + shortUrl + 
          '" style="color:inherit; text-decoration:none; font-weight:bold;">view your profile!</a></div><img src="' + 
          shortUrl + '.qrcode" style="height:110px; width:110px;" />'; 
     } 
    }); 

    this.getLblQrCodeHtml().setData({ QrCodeHtml: qrCodeHtml }); 
    Ext.Viewport.animateActiveItem(this.getProfileView(), this.slideLeftTransition); 
} 

回答

1

感謝Goyuix,只是我需要的東西!這裏的解決方案:

onMyProfileCommand: function() { 
var api_username = "o_xxxxx"; 
var api_key = "R_xxxxxxxxxx"; 
var long_url = "http://google.com"; 
var controller = this; 

Ext.data.JsonP.request({ 
    url: 'https://api-ssl.bitly.com/v3/shorten', 
    callbackKey: 'callback', 
    params: { 
     login: api_username, 
     apiKey: api_key, 
     longUrl: long_url 
    }, 
    success: function (result, request) { 
     var shortUrl = result.data.url; 
     var qrCodeHtml = '<div style="font-size:15px; margin-bottom:5px;">Friends can scan this to <a href="' + shortUrl + 
         '" style="color:inherit; text-decoration:none; font-weight:bold;">view your profile!</a></div><img src="' + 
         shortUrl + '.qrcode" style="height:110px; width:110px;" />'; 

     controller.getLblQrCodeHtml().setData({ QrCodeHtml: qrCodeHtml }); 
    } 
}); 

Ext.Viewport.animateActiveItem(controller.getProfileView(), this.slideLeftTransition); 

}

0

的JSONP請求將進行異步 - 也許你只需要最後兩個語句移動成功事件處理程序的內部?否則,請求將排隊併發布,最後兩行將在qrCodeHtml變量已設置爲您正在嘗試生成的HTML之前立即發佈。

您將需要從外部函數捕捉this變量,不遠的地方你其它變量的定義上你的成功處理程序中使用,這樣的話:

var that = this; 

然後更新您的成功處理程序使用在that變量,一旦您的要求回來進行更新:

success: function (result, request) { 
    var shortUrl = result.data.url; 
    qrCodeHtml = '<div style="font-size:15px; margin-bottom:5px;">Friends can scan this to <a href="' + shortUrl + 
         '" style="color:inherit; text-decoration:none; font-weight:bold;">view your profile!</a></div><img src="' + 
         shortUrl + '.qrcode" style="height:110px; width:110px;" />'; 
    that.getLblQrCodeHtml().setData({ QrCodeHtml: qrCodeHtml }); 
    Ext.Viewport.animateActiveItem(this.getProfileView(), this.slideLeftTransition); 
    } 
}); 
+0

很好的建議。我試過了,我收到了這個錯誤:Uncaught TypeError:Object [object Window]沒有方法'getLblQrCodeHtml'(我得到了getProfileView()的同樣的錯誤)由於某些原因,這些對象是不可訪問的。 – James

+0

完美,正是我所需要的!我在下面發佈瞭解決方案。 – James

相關問題