2015-10-16 82 views
0

我的JSON数组是遵循了如何使用骨干提取名称和临时骨干获取数据

{ 
    "message": "accurate", 
    "cod": "200", 
    "count": 10, 
    "list": [ 
    { 
     "id": 495260, 
     "name": "Shcherbinka", 
     "coord": { 
     "lon": 37.559719, 
     "lat": 55.499722 
     }, 
     "main": { 
     "temp": 274.72, 
     "pressure": 1033, 
     "humidity": 93, 
     "temp_min": 272.04, 
     "temp_max": 277.04 
     }, 
     "dt": 1444968405, 
     "wind": { 
     "speed": 2, 
     "deg": 300 
     }, 
     "sys": { 
     "country": "" 
     }, 
     "clouds": { 
     "all": 76 
     }, 
     "weather": [ 
     { 
      "id": 803, 
      "main": "Clouds", 
      "description": "broken clouds", 
      "icon": "04d" 
     } 
     ] 
    }, 

我的JavaScript如下:

<script> 


     $(function() { 
       var Profile = Backbone.Model.extend(); 

       var ProfileList = Backbone.Collection.extend({ 
        model: Profile, 
        url: "http://api.openweathermap.org/data/2.5/find?lat=55.5&lon=37.5&cnt=10&appid=bd82977b86bf27fb59a04b61b657fb6f",  
        parse : function(response){ 
        weather_data = response.list 
        console.log(weather_data) 
         return weather_data; 
        } 
       }); 

       var ProfileView = Backbone.View.extend({ 
        el: "#profiles", 
        template: _.template($('#profileTemplate').html()), 
        render: function(eventName) { 
         _.each(this.model.models, function(profile){ 
          var profileTemplate = this.template(profile.toJSON()); 
          $(this.el).append(profileTemplate); 
         }, this); 

         return this; 
        } 
       }); 

       var profiles = new ProfileList(); 
       profiles.fetch(); 
       var profilesView = new ProfileView({model: profiles}); 
       profiles.fetch({ 
        success: function() { 
         profilesView.render(); 
        } 
       }); 

      }); 
     </script> 

如何从获得Nametemperature在Json上面,它们在Json对象内部是不同的Json对象

+0

你需要其他的数据藏汉属性?在应用程序的生命周期中属性是否改变?你需要事件监听器吗?你需要将模型保存到服务器吗? –

回答

1

如何从上面的Json

如果你的意思是访问“名”和“温度”获得的名称和温度,可以一次将他们的模型设定如下访问它们。

model.get("name"); //"Shcherbinka" 
model.get("main").temp; //274.72