1

我无法在我的主干应用程序中加载外部下划线模板。内联模板(注释掉)工作得很好,但外部没有。

$.app.ObservationSummaryView = Backbone.View.extend({
    tagName: 'div',
    className: 'observation_summary_view',
    // template: _.template('ID: <%= id %><p>Comments:<%= comments.length %>'),
    template: _.template($('#tpl_observation_summary').html()),
    initialize: function(){
        _.bindAll(this, 'render', 'on_click');
        this.model.bind('change', this.render);
    },
    render: function(){
        return $(this.el).html(this.template(this.model.toJSON()));
    },
    on_click: function(e){
        $.app.app.navigate('observation/' + this.model.get('id') + '/', {trigger: true});
    }
});

HTML/模板:

<script type="text/template" id="tpl_observation_summary">
<%= id %>
</script>
<script type="text/javascript">
    $(document).ready(function() {
    (function($) {
    $.app.bootstrap();
})(jQuery);
});
</script>

错误:

Uncaught TypeError: Cannot call method 'replace' of undefined underscore.js:1131
Uncaught TypeError: Object #<Object> has no method 'bootstrap'

我猜这与我的应用程序在一个函数中有关,但我不确定如何解决这个问题。

4

1 回答 1

1

在你调用的那一刻$('#tpl_observation_summary').html(),DOM 似乎还没有准备好,你得到null了 的第一个参数_.template,因此导致 underscoreJS 无法处理不是一个字符串。

然后bootstrap无法定义该方法,因此不能作为 的函数调用$.app

模板初始化可以包含在initializeBackbone 视图的第一次调用中。

于 2013-04-15T20:34:55.353 回答