未捕获类型错误:不能调用方法'取代'Backbone.js的未定义的 [英] uncaught TypeError: Cannot call method 'replace' of undefined backbone.js

查看:150
本文介绍了未捕获类型错误:不能调用方法'取代'Backbone.js的未定义的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图开发使用Backbone.js的一个简单的RSS应用程序。我使用这个Backbone.js的教程 。我在收到以下错误,在2号线(模板),定义模板时。
可有人还告诉我,为什么是标签名:礼的教程中定义

I 'm trying to develop a simple RSS app using backbone.js. I 'm using this backbone.js tutorial. I 'm getting the following error, on line 2(template), when defining the template. Can someone also tell me why is tagName: "li" defined in the tutorial?

未捕获类型错误:不能调用方法'取代'未定义
  Backbone.js的

uncaught TypeError: Cannot call method 'replace' of undefined backbone.js

Javscript

window.SourceListView = Backbone.View.extend({
    tagName:"li",
    template: _.template($('#tmpl_sourcelist').html()),

    initialize:function () {
        this.model.bind("change", this.render, this);
        this.model.bind("destroy", this.close, this);
    },

    render:function (eventName) {
        $(this.$el).html(this.template(this.model.toJSON()));
        return this;
    },

    close:function () {
        $(this.el).unbind();
        $(this.el).remove();
    }
});

HTML

 <script type="text/template" id="tmpl_sourcelist">
                        <div id="source">
                        <a href='#Source/<%=id%>'<%=name%></a>
                        </div>
                </script>

感谢

推荐答案

你让你的错误就在这里:

You're getting your error right here:

template: _.template($('#tmpl_sourcelist').html()),

_。模板的一部分的内部需要调用<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace\"><$c$c>String#replace上的方式来生产编译模板函数未编译模板文本。特定错误通常意味着你有效地这样说:

Part of _.template's internals involves calling String#replace on the uncompiled template text on the way to producing the compiled template function. That particular error usually means that you're effectively saying this:

_.template(undefined)

如果没有 #tmpl_sourcelist 的时候你说DOM $('#tmpl_sourcelist')。HTML()

That can happen if there is no #tmpl_sourcelist in the DOM when you say $('#tmpl_sourcelist').html().

有几个简单的解决方案:

There are a few simple solutions:


  1. 调整你的&LT;脚本&GT; 为了让你的 #tmpl_sourcelist 来尝试加载你的看法了。

  2. 创建编译模板函数视图的初始化而不是在视图中的类​​的定义:

  1. Adjust your <script> order so that your #tmpl_sourcelist comes before you try to load your view.
  2. Create the compiled template function in your view's initialize instead of in the view's "class" definition:

window.SourceListView = Backbone.View.extend({
    tagName:"li",
    initialize:function () {
        this.template = _.template($('#tmpl_sourcelist').html());
        //...


至于标签名得好,精细的手工有这个说:

view.el

[...] this.el 从创建视图的标签名的className ID 属性属性,如果指定。如果不是,是一个空 DIV

[...] this.el is created from the view's tagName, className, id and attributes properties, if specified. If not, el is an empty div.

所以,有这个在您的视图:

So having this in your view:

tagName: 'li'

意味着骨干会自动创建一个新的&LT;李方式&gt; 元素作为视图的

这篇关于未捕获类型错误:不能调用方法'取代'Backbone.js的未定义的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆