How do I properly store a javascript template, so that it isn't instantiated multiple times

后端 未结 6 397
一整个雨季
一整个雨季 2021-02-03 12:44

I\'m using Backbone and therefore Underscore to render my templates. My templates get rendered in

6条回答
  •  抹茶落季
    2021-02-03 13:06

    You could muck around with prototype.template by hand and compile the template the first time you create an instance of your view. Something like this:

    initialize: function() {
        if(!this.constructor.prototype.template)
            this.constructor.prototype.template = _.template($("#ItemTemplate").html());
        this.render();
    }
    

    Demo: http://jsfiddle.net/ambiguous/e6y3F/

    The trick is to get your hands on the right prototype.

提交回复
热议问题