javascript UnderscoreJS Uncaught TypeError:无法调用未定义的方法“替换”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15650544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:32:44  来源:igfitidea点击:

UnderscoreJS Uncaught TypeError: Cannot call method 'replace' of undefined

javascriptbackbone.jsunderscore.js

提问by Nonconformist

In my Backbone view I have:

在我的 Backbone 视图中,我有:

noteTemplate: _.template($('#note-template').html()),

noteTemplate: _.template($('#note-template').html()),

Which is throwing this error. The template is:

这是抛出这个错误。模板是:

<script type="text/template" id="note-template">
    <div class="reminder">
        <div class="reminder-hover">
            <div class="title"><%= title %></div>
            <div class="created">Created 3 days ago <span class="pull-right"> In 3 hours</span></div>
        </div>
        <span class="id" style="display: none;"><%= id %></span>
    </div>
</script>

I am confused because this works in my console:

我很困惑,因为这在我的控制台中有效:

>> _.template($('#note-template').html());
function (n){return e.call(this,n,w)}

>> _.template($('#note-template').html());
function (n){return e.call(this,n,w)}

Here is the the full code:

这是完整的代码:

App.Views.Index = Backbone.View.extend({

    el: $("div.reminders"),
    todays : $("span.today"),
    tomorrows : $("span.tomorrow"),
    weeks : $("span.week"),
    all_times : $("span.all-time"),

    noteTemplate: _.template($('#note-template').html()),


    events: {
        "click .reminder" : "editNote",
        "click .newNote"  : "newNote"
    },

    initialize : function() { 
        _.bindAll(this, 'render');
        this.notes = this.options.notes;
        this.listenTo(this.model, 'change', this.render);
    },

    render : function() {
        // Hide things related to editing a note 
        this.$("[name=content]").hide().val("");
        this.$("[name=title]").hide().val("");
        this.$("save1").hide();
        this.$("close1").hide();

        // Fill in content
        this.$el.html( this.noteTemplate( this.model.toJSON() ) );
        this.$todays.html( collection.getTodays.length );
        this.$tomorrows.html( collection.getTomorrows.length );
        this.$weeks.html( collection.getWeeks.length );
        this.$all_times.html( collection.getAllTimes.length );
        return this;
    },

    editNote : function() {
        this.goTo("notes/"+this.model.id);
    },

    newNote : function(){
        this.goTo("newNote");
    }

});

回答by numbers1311407

Rather than trying to cache the note template HTML when you define the method, do it when you initialize the view.

与其在定义方法时尝试缓存笔记模板 HTML,不如在初始化视图时进行缓存。

initialize : function() { 
    // ...
    this.nodeTemplate = _.template($('#note-template').html());
}

It's highly likely that you're defining the View before the DOM (and thus the template) is loaded.

您很有可能在加载 DOM(以及模板)之前定义视图。

回答by Julio Cesar Canares Garcia

Another solution is move the tag script above of call view , like this (I'm using jade)

另一个解决方案是将标记脚本移动到 call view 上方,就像这样(我正在使用 jade)

script#page-thumb(type='text/template')
    .page-thumb-container.relative
      .page-thumb
        .image
          figure
            img(src='{{ image }}' , width='80px',height='60px' , data-id='{{ id }}')
          span.title {{ title }}
      .page-dragger.absolute

script(src='js/src/views/PageThumbView.js')