Underscore template throwing variable not defined error

后端 未结 2 1787
余生分开走
余生分开走 2020-11-22 05:53

I\'ve watched some videos on the topic of backbone js. This is an example straight from the video. It is from 2012, so I\'m thinking backbone rules/library have changed, but

2条回答
  •  再見小時候
    2020-11-22 06:37

    This can be useful

    1: If you have more then one template or sometime you are using external template so it can be useful for you inside method you can write reusable code

    var V = Backbone.View.extend({
        el:'body',
        temp: function (str) {
    
            // reusable code
            return _.template(str);
        },
        render: function () {
            var data = { lat: -27, lon: 153 };
    
            // calling your view method temp        
            var tmpl = this.temp('<%= lat %> <%= lon %>');
            this.$el.html(tmpl(data));
    
            return this;
        }
    });
    
    var v = new V();
    v.render();
    

提交回复
热议问题