Backbone.View没有显示。但是console.log看到了我的对象

时间:2015-09-25 09:19:57

标签: javascript html backbone.js

这是我的HTML

<script type="text/template" id="productTemplate">
            <p><%= title %></p>
                <h1><%= title %></h1>
                <img src="<%= img %>" alt="<%= title %>" />
                <p><%= text %></p>
            </script>

这是我的JS代码

var Product = Backbone.Model.extend();
var Products = Backbone.Collection.extend({
model: Product,
url: '/*there server url*/'
});
var ProductView = Backbone.View.extend({
tagName:'li',
className:'product-container',
initialize:function(){
this.render(); 
},
template:  _.template( $('#productTemplate').html() ),
render: function () {
    console.log(this.model.toJSON());
    this.$el.html( this.template(this.model.toJSON()));
   return this;
}
});
var Ajax =  new Products();
var ProductsView = Backbone.View.extend({
tagName:'ul',
initialize:function(initialProduct){
    this.collection = Ajax;
    this.collection.fetch({reset: true});
    this.render();
    this.listenTo( this.collection, 'add', this.renderProduct );
    this.listenTo( this.collection, 'reset', this.render );
    },
render:function(){
    this.collection.each(function(item){
        this.renderProduct(item);     
        },this);
    },
renderProduct: function(item){
    var productView = new ProductView({
        model:item
        });
    this.$el.append(productView.render(item));
    }
});
$(function(){
var productsView = new ProductsView;
});

的console.log(this.model.toJSON()); 有观点

  

对象{id:1,标题:&#34; product1&#34;,img:&#34; img1.png&#34;,文字:&#34; lorem ipsum 1&#34;}   对象{id:1,title:&#34; product1&#34;,img:&#34; img1.png&#34;,text:&#34; lorem ipsum   1&#34;}

0 个答案:

没有答案