Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 主干视图和模板_Backbone.js_View - Fatal编程技术网

Backbone.js 主干视图和模板

Backbone.js 主干视图和模板,backbone.js,view,Backbone.js,View,如何从该模型或集合创建视图和模板?我可以用控制台记录我想要的数据。我被视图和模板部分卡住了。谢谢 var Weather = Backbone.Model.extend({ urlRoot: "http://api.openweathermap.org/data/2.5/weather?q=New%20York&mode=json&units=imperial", initialize: function (response) { console.log(resp

如何从该模型或集合创建视图和模板?我可以用控制台记录我想要的数据。我被视图和模板部分卡住了。谢谢

var Weather = Backbone.Model.extend({
urlRoot: "http://api.openweathermap.org/data/2.5/weather?q=New%20York&mode=json&units=imperial",
initialize: function (response) {
        console.log(response.wind.speed);    
console.log(response.main.temp);     
 console.log(response.name);
 console.log(response.main.temp_min);
 console.log(response.main.temp_max);
 //console.log();
 return response;   
}

});

var WeatherCollection = Backbone.Collection.extend({
 model: Weather,
 url: 'http://api.openweathermap.org/data/2.5/weather?q=New%20York&mode=json&units=imperial',
 parse: function(response) {
        console.log(response.wind.speed);    
console.log(response.main.temp);     
 console.log(response.name);
 console.log(response.main.temp_min);
 console.log(response.main.temp_max);
 //console.log();
 return response;
 }
 });

首先,我可能会这样做:

var WeatherItemView = Backbone.View.extend({
  template: _.template($('#weather-template').html()),
  render: function () {
    var content = this.template({
      weather: this.model
    });

    this.$el.html(content);
    return this;
  }
});

var WeatherListView = Backbone.View.extend({
  initialize: function () {
    this.listenTo(this.collection, 'sync', this.render);
  },
  render: function () {
    this.collection.each(function (weather) {
      var subView = new WeatherItemView({
        model: weather
      });
      this.$el.append(subView.render().$el);
    }, this);

    return this;
  }
});

$(document).ready(function () {
  var weathers = new WeatherCollection();
  weathers.fetch(); // assuming its accessing an api endpoint.

  var weathersView = new WeatherListView({
    collection: weathers
  });
  $('body').html(weathers.render().$el);
});

<!-- template for one weather item view -->
<script id='weather-template' type="text/template">
  <%= weather.escape('name') %>
</script>
var WeatherItemView=Backbone.View.extend({
模板:35;.template($('#天气模板').html()),
渲染:函数(){
var content=this.template({
天气:这是一款
});
这是.$el.html(内容);
归还这个;
}
});
var WeatherListView=Backbone.View.extend({
初始化:函数(){
this.listenTo(this.collection,'sync',this.render);
},
渲染:函数(){
此.集合.每个(功能(天气){
var子视图=新建WeatherItemView({
型号:天气
});
这个.el.append(subView.render().el);
},这个);
归还这个;
}
});
$(文档).ready(函数(){
var weathers=新的WeatherCollection();
weathers.fetch();//假设它正在访问api端点。
var weathersView=新WeatherListView({
收藏:weathers
});
$('body').html(weathers.render().$el);
});