简体   繁体   English

骨干渲染视图多次

[英]Backbone render view multiple times

I have a backbone.js app where I have a view like: 我有一个beta.js应用程序,其中有一个类似这样的视图:

var StoreView = Backbone.View.extend({
el: ".container",
initialize: function() {
    this.render();
},

events: {
    "click #addProduct": "addProduct",
},

render: function() {
    this.$el.html(render("index"));
},

addProduct: function(){
    var productView = new StoreProductView({
        el: $('#products')
    });
    productView.render();
}
});

and my StoreProductView looks like: 和我的StoreProductView看起来像:

var StoreProductView = Backbone.View.extend({
render: function () {

    this.$el.html(render("products/product"));

    return this;
}
});

So when I click #addProduct, a new StoreProductView template is called. 因此,当我单击#addProduct时,将调用一个新的StoreProductView模板。 But if you hit #addProduct again, nothing happens. 但是,如果再次点击#addProduct,则什么也不会发生。 How can I get it so that for each time #addProduct is hit, a new StoreProductView is rendered? 如何获得它,以便每次点击#addProduct时,都会呈现一个新的StoreProductView So if I hit it x number of times, there is x StoreProductView s? 因此,如果我按x次点击,就有x个StoreProductView

you need to change this line: 您需要更改此行:

this.$el.html(render("products/product"));

to this for example: 例如:

this.$el.append(render("products/product"));

because every time you substitute the html and nw you don't append, only remove html content and insert the new one. 因为每次您替换html和nw时都不会追加,所以仅删除html内容并插入新内容。 With append() you can add anoter product. 使用append()可以添加注释器产品。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM