简体   繁体   English

未捕获的TypeError:无法调用未定义的ribs.js的方法“替换”

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

I 'm trying to develop a simple RSS app using backbone.js. 我正在尝试使用Beta.js开发一个简单的RSS应用程序。 I 'm using this backbone.js tutorial . 我正在使用这个ribs.js 教程 I 'm getting the following error, on line 2(template), when defining the template. 在定义模板时,在第2行(模板)上出现以下错误。 Can someone also tell me why is tagName: "li" defined in the tutorial? 谁能告诉我为什么在教程中定义了tagName:“ li”?

uncaught TypeError: Cannot call method 'replace' of undefined backbone.js 未捕获的TypeError:无法调用未定义的ribs.js的方法“替换”

Javscript 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 的HTML

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

thanks 谢谢

You're getting your error right here: 您在这里遇到了错误:

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

Part of _.template 's internals involves calling String#replace on the uncompiled template text on the way to producing the compiled template function. _.template的内部结构的一部分涉及在生成编译的模板函数的方式上,对未编译的模板文本调用String#replace That particular error usually means that you're effectively saying this: 该特定错误通常意味着您实际上是在说这句话:

_.template(undefined)

That can happen if there is no #tmpl_sourcelist in the DOM when you say $('#tmpl_sourcelist').html() . 当您说$('#tmpl_sourcelist').html()时,如果DOM中没有#tmpl_sourcelist ,则会发生这种情况。

There are a few simple solutions: 有一些简单的解决方案:

  1. Adjust your <script> order so that your #tmpl_sourcelist comes before you try to load your view. 调整您的<script>顺序,以使#tmpl_sourcelist出现在您尝试加载视图之前。
  2. Create the compiled template function in your view's initialize instead of in the view's "class" definition: 在视图的initialize而不是在视图的“类”定义中创建已编译的模板函数:

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

As far as tagName goes, the fine manual has this to say: tagName而言, 精美的手册说:

el view.el 埃尔 view.el

[...] this.el is created from the view's tagName , className , id and attributes properties, if specified. [...] this.el是从视图的tagNameclassNameidattributes属性(如果指定)创建的。 If not, el is an empty div . 如果不是,则el为空div

So having this in your view: 因此,在您看来:

tagName: 'li'

means that Backbone will automatically create a new <li> element as your view's el . 表示Backbone将自动创建一个新的<li>元素作为视图的el

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Backbone.js-未捕获的TypeError:无法调用未定义的方法&#39;receivedEvent&#39; - Backbone.js - Uncaught TypeError: Cannot call method 'receivedEvent' of undefined 未捕获的TypeError:无法调用未定义的方法&#39;on&#39; - Backbone.js - Uncaught TypeError: Cannot call method 'on' of undefined - Backbone.js TypeError:无法调用null的方法“ replace”-Backbone.js - TypeError: Cannot call method 'replace' of null - Backbone.js 呈现lobb.js集合视图时遇到问题。 在我的初始化函数中得到“ Uncaught TypeError:无法调用未定义的方法&#39;on&#39;” - Having trouble rendering backbone.js collection view. Getting “Uncaught TypeError: Cannot call method 'on' of undefined” in my initialize function 未捕获的TypeError:无法调用undefined underscore.js的方法&#39;replace&#39; - Uncaught TypeError: Cannot call method 'replace' of undefined underscore.js 未捕获的TypeError:无法调用未定义的方法“替换” - Uncaught TypeError: Cannot call method 'replace' of undefined 未捕获的TypeError:无法调用未定义的方法&#39;replace&#39; - Uncaught TypeError: Cannot call method 'replace' of undefined 未捕获的TypeError:无法调用未定义的方法“ get”:BackBone - Uncaught TypeError: Cannot call method 'get' of undefined : BackBone Backbone - Uncaught TypeError:无法调用未定义的方法&#39;unbind&#39; - Backbone - Uncaught TypeError: Cannot call method 'unbind' of undefined Uncaught TypeError:无法在主干中调用未定义的方法“ toJSON” - Uncaught TypeError: Cannot call method 'toJSON' of undefined in backbone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM