2013-03-21 47 views
0

我正在extjs.I中工作,我想查找特定城市的天氣。我從「http://api.wunderground.com/api/4ab310c7d75542f3/geolookup/conditions/q/IA/pune.json」得到了參考。它通過在html文件中寫入以下代碼給出與json格式的給定城市有關的所有信息 -如何將jquery代碼集成到extjs4中

<script 
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"> 

</script> 

<script> 
    jQuery(document).ready(function($) { 
         $.ajax({ 
            url : "http://api.wunderground.com/api/4ab310c7d75542f3/geolookup/conditions/q/IA/Pune.json", 
            dataType : "jsonp", 
            success : function(parsed_json) { 
             var location = parsed_json['location']['city']; 
             var temp_c = parsed_json['current_observation']['temperature_string']; 
             alert("Current temperature in " 
               + location + " is: " + temp_c); 
            } 
           }); 
        }); 
</script> 

它的工作正常。現在我想將這個jquery代碼集成到extjs的控制器中。那麼有人可以指導我如何在extjs中集成jquery代碼嗎?

回答

2

以下extjs代碼與您的代碼相同,因此您可以在控制器內部使用它。

Ext.data.JsonP.request({ 
    url:"http://api.wunderground.com/api/4ab310c7d75542f3/geolookup/conditions/q/IA/Pune.json", 
    success : function(parsed_json) { 
     var location = parsed_json['location']['city']; 
     var temp_c = parsed_json['current_observation']['temperature_string']; 
     alert("Current temperature in " + location + " is: " + temp_c); 
    } 
}); 
+0

感謝很多先生的回覆。我已經包含上面的代碼在controller.But它給我的錯誤 - 「TypeError:Ext.data.JsonP是未定義的」那麼我需要做什麼改變?你能否指導我 – user1722857 2013-04-10 05:51:54

+0

你應該在使用之前加載[Ext.data.JsonP](http://docs.sencha.com/ext-js/4-2/#!/api/Ext.data.JsonP)加載您可以用於示例[此代碼]的類(http://jsfiddle.net/s9Lf6/)。 Ext.require('Ext.data.JsonP',function(){*您的代碼*/ }); – Volodymyr 2013-04-10 19:57:43

+0

Thanx for reply sir ...它的工作..我想再問一件事。我如何動態更改城市名稱。即假設我有變量存儲城市名稱。所以如何更改url-「http://api.wunderground.com/api/4ab310c7d75542f3/geolookup/conditions/q/IA/pune.json」,以便城市名稱將被該變量替換。我們將根據變量所在的城市名稱獲取城市信息。請指導我 – user1722857 2013-04-20 12:25:26

0

ExtJS和jQuery(都有自己的命名空間)之間沒有衝突。只要jQuery在app.js之前加載,就可以在ExtJS控制器中使用jQuery。自從ExtJS 4.x開始,我使用jQuery和ExtJS,並且從來沒有任何問題。

0

在sencha touch和extjs中,所有您的javascript代碼都將放置在您的app.js文件或其他類js文件中。你永遠不會把這樣的代碼放在html中的腳本標籤中。如果您想在加載應用程序之前撥打該位置或首先將其放入您的應用程序啓動功能或直接放在上面。

//DO YOUR AJAX CALL HERE BEFORE YOUR APPLICATION LAUNCHES 

    Ext.application({ 
     name: 'MyApp', 
     launch: function() { 

      //OR INSIDE THE LAUNCH FUNCTION 
     } 
    });