2012-07-17 116 views
1

我通过peepcode backbone.js基础教程工作,并且据我所知,我的代码与截屏相同,但是我的控制台行为非常不同。backbone.js fetch()返回对象而不是子

我的Chrome控制台(用于screecast)会产生此结果。

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) 

回答

1

你的代码是好的,示例/截屏有错误,或者使用旧backbonejs实现,并使用较旧的镀铬因此孩子VS inherits.child输出。

  • 获取应返回的对象 - 这是一个jQuery Deferred对象,你可以用它来解决成功和错误回调(签出更多的jQuery的jQuery的API文档推迟 - !伟大的东西)
  • 没有Backbone.Collection模型方法 - 它是一个模型实例的属性,应该访问albums.models而不是albums.models()
+0

欢迎来到快活的主干世界:) – 2012-07-17 13:01:39

相关问题