javascript - 类型错误 : Cannot call method 'replace' of null - Backbone. js

标签 javascript jquery model-view-controller backbone.js

我正在尝试使用 peepcode 做一个教程 Backbone 项目,但我卡住了。

我正在尝试通过从集合创建新 View 来从控制台渲染 View 。这是我的代码

(function($) {

    window.Album = Backbone.Model.extend({

        isFirstTrack: function(index) {
            return index == 0; 
        },

        isLastTrack: function(index) {
            return index >= this.get('tracks').length - 1; 
        },

        trackUrlAtIndex: function(index) {
            if (this.get('tracks').length >= index) {
                return this.get('tracks')[index].url;
           }
            return null;
        }

    }); 

    window.Albums = Backbone.Collection.extend({
        model: Album,
        url: '/albums'
    });

    window.Album = Backbone.Model.extend({});

    window.AlbumView = Backbone.View.extend({
        tagName: 'li',
        className: 'album',


        initialize: function() {
            _.bindAll(this, 'render');
            this.model.bind('change', this.render);

            this.template = _.template($('#album-template').html());
        },

        render: function() {
            var renderedContent = this.template(this.model.toJSON());
            $(this.el).html(renderedContent);
            return this;
        }
    });

    window.LibraryAlbumView = AlbumView.extend({

    });

    window.LibraryView = Backbone.View.extend({
        tagName: 'section',
        className: 'library',

        initialize: function () {
            _.bindAll(this, 'render');
            this.template = _.template($('#library-template').html());
            this.collection.bind('reset', this.render);
        },

        render: function() {
            var $albums,
                collection = this.collection;

            $(this.el).html(this.template({}));
            $albums = this.$(".albums");
            collection.each(function(album) {
                var view = new LibraryAlbumView({
                    model: album,
                    collection: collection
                });
                $albums.append(view.render().el);
            });
            return this;
        }
    });
})(jQuery);

当我输入libraryView = new LibraryView({collection:library}) 在控制台中我得到以下响应:

类型错误:无法调用 null 的“替换”方法

谁能帮帮我吗?

最佳答案

您的标记中很可能不存在库 View 的元素。

这应该是真的

$('#library-template').length == 1;

如果您这样做,则可能会发生此错误:

 _.template(null)
 _.template(undefined);

对不存在的元素调用 .html() 将返回 undefined

关于javascript - 类型错误 : Cannot call method 'replace' of null - Backbone. js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16501372/

相关文章:

javascript - Gulp.js,保存文件时观察任务运行两次

javascript - 无法初始化 Foundation 5 上的轨道

ios - 模型和 Controller 之间的通信

c# - 如何在 C# 中创建一个完美的 Singleton 类?

java - 在 Java/JavaFX 中实现可以容纳多种数据类型的通用列表

javascript - 如何在 ember-cp-validations 中验证复选框、选择框和单选按钮

Javascript 到 Java Applet 通信

javascript - 如何在 Svelte 中动态绑定(bind)事件?

javascript - 在 Angular Js 中重新加载后,如何将复选框(选中或未选中)从 javascript Controller 更新为 html 页面?

javascript - Framework 7 onclick 事件绑定(bind)不起作用