繁体   English   中英

骨架.js fetch()返回对象而不是子对象

[英]backbone.js fetch() returning object instead of child

我遍历了peepcode boundary.js基础教程,据我所知,我的代码与截屏视频相同,但是控制台的行为却大不相同。

我的Chrome控制台(在广播中使用)产生了此结果。

albums = new Albums()
child
albums.fetch()
Object
albums.models()
TypeError: Property 'models' of object [object Object] is not a function

截屏控制台看起来像这样

albums = new Albums()
inherits.child
albums.fetch()
inherits.child
albums.models()
[ inherits.child, inherits.child ]

我完全迷失了在哪里崩溃。 是我的代码(请参阅下文),我的浏览器还是其他工具?

(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.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;
    }

});

})(jQuery)

您的代码很好,示例/截屏有错误,或者正在使用较旧的或更老的chrome实施,并且正在使用较旧的chrome,因此child VSInherits.child输出。

  • fetch应该返回对象-这是一个jQuery延迟对象,您可以使用它来解决成功和错误回调(查看有关jQuery API文档中延迟的jQuery的更多信息-很棒的东西!)
  • 没有Backbone.Collection模型方法-它是模型实例上的属性,应该由albums.models而不是albums.models()

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM