Using Handlebars with Backbone

后端 未结 4 629
隐瞒了意图╮
隐瞒了意图╮ 2020-12-22 19:20

I am learning Backbone/Handlebars/Require. I have looked all over online and on SO - are there any tutorials or websites that you can direct me to that would provide helpful

4条回答
  •  礼貌的吻别
    2020-12-22 19:55

    I would prefer to compile the template once (during initialize), that way you avoid to recompile the template with every render. Also, you need to pass the model to the compilated template in order to generate the HTML:

    SearchView = Backbone.View.extend({
      initialize: function(){
        // Compile the template just once
        this.template = Handlebars.compile($("#search_template").html());
        this.render();
      },
      render: function(){
        // Render the HTML from the template
        this.$el.html(this.template(this.model.toJSON()));
        return this;
      }
    });
    

提交回复
热议问题