javascript之使用下划线外部模板在主干中设置模型内容

xiaohuochai 阅读:290 2026-07-31 16:07:56 评论:0

这是我的主干代码,其中我使用下划线模板,现在工作正常,我正在尝试将该模板设为外部并从那里加载

这是我的示例主干代码

Backbone 应用

<div class="list"></div> 
 
<script id="personTemplate" type="text/template"> 
    <strong><%= name %></strong> (<%= age %>) - <%= occupation %> 
</script> 
<script type="text/javascript"> 
var Person = Backbone.Model.extend({ 
    defaults: { 
        name: 'Guest User', 
        age: 23, 
        occupation: 'worker' 
    } 
}); 
 
var PersonView = Backbone.View.extend({ 
   tagName: 'li', 
 
    template: _.template( $('#personTemplate').html()), 
 
    initialize: function(){ 
        this.render(); 
    }, 
 
    render: function(){ 
        this.$el.html( this.template(this.model.toJSON())); 
    } 
}); 
var person = new Person;  // a person object created... 
//person.set('name', 'abhi'); 
var personView = new PersonView({ model: person }); 
personView.el   // ---->; You can call this method and it will display the view.. 
$(document.body).html(personView.el); 
</script> 
</body> 
</html>  

真正需要的是我将放置此模板<strong><%= name %></strong> (<%= age %>) - <%= occupation %>在外部文件中。我对它的扩展了解不多,我认为它可以是一个 html 文件并从那里加载模板,我研究了它并找到了一些示例代码并尝试了类似的东西

<script type="text/javascript"> 
_.mixin({templateFromUrl: function (url, data, settings) { 
    var templateHtml = ""; 
    this.cache = this.cache || {}; 
 
    if (this.cache[url]) { 
        templateHtml = this.cache[url]; 
        alert(templateHtml); 
    } else { 
        $.ajax({ 
            url: url, 
            method: "GET", 
            async: false, 
            success: function(data) { 
                templateHtml = data; 
                alert(templateHtml); 
            } 
        }); 
 
        this.cache[url] = templateHtml; 
    } 
 
    return _.template(templateHtml, data, settings); 
}}); 
var Person = Backbone.Model.extend({ 
    defaults: { 
        name: 'Guest User', 
        age: 23, 
        occupation: 'worker' 
    } 
}); 
 
var PersonView = Backbone.View.extend({ 
   tagName: 'li', 
 
   template:_.templateFromUrl("templatefile1.html", {"name": "value","age":10}) , 
   // template:_.templateFromUrl("templatefile1.html", this.model.toJSON()) , 
  //template: _.template( $('#personTemplate').html()), 
    initialize: function(){ 
        this.render(); 
    }, 
 
    render: function(){ 
    alert(this.template); 
        this.$el.html( this.template); 
    } 
}); 
var person = new Person;  // a person object created... 
//person.set('name', 'abhi'); 
var personView = new PersonView({ model: person }); 
personView.el   // ---->; You can call this method and it will display the view.. 
$(document.body).html(personView.el); 
</script> 

但是当我像这样调用模板时 template:_.templateFromUrl("templatefile1.html", {"name": "value","age":10}) , 这是工作 但是当我尝试将传递模型传递给该模板时,如下所示

模板:_.templateFromUrl("templatefile1.html", this.model.toJSON())

我遇到了类似的异常

Uncaught TypeError: Cannot read property 'toJSON' of undefined  

如何解决这个问题

请您参考如下方法:

你说的是这样的话:

var PersonView = Backbone.View.extend({ 
    //... 
    template: _.templateFromUrl("templatefile1.html", this.model.toJSON()), 
    //... 
}); 

您认为 this 在这种情况下是什么?它肯定不会是您 View 的实例,因为尚未创建任何实例。 this 将是该级别的 window,而 window 不会有有用的 model 属性。您需要推迟该调用,直到使用 model 属性创建 View 实例。

您可以将template设为一个方法:

template: function() { 
    return _.templateFromUrl('templatefile1.html', this.model.toJSON()); 
} 

然后在适当的时间调用它:

render: function(){ 
    this.$el.html(this.template()); 
    return this; // <----- Standard practise so you can x.append(v.render().el) 
} 

我建议不要将 async:false 与 $.ajax 一起使用,这对人们来说是一件令人讨厌的事情。相反,您应该向 templateFromUrl 提供一个回调函数,当模板从服务器传入时,它可以调用该函数。

如果您要经常做这种事情,那么您可以向基本 View 添加一个模板函数:

var BaseView = Backbone.View.extend({ 
    template: function() { 
        // This assumes that you have exactly one of `this.model` and 
        // `this.collection`, you can do other things here if that doesn't 
        // work in your world. 
        var data_source = this.model || this.collection; 
        return _.templateFromUrl(this.template_name, data_source.toJSON()); 
    } 
}); 

然后在您的PersonView中使用它:

var PersonView = BaseView.extend({ 
    template_name: 'templatefile1.html', 
    render: function() { 
        this.$el.html(this.template()); 
        return this; 
    }, 
    //... 
}); 


标签:JavaScript
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号