I working through the peepcode backbone.js basics tutorial and as far as I can tell my code is identical to the screencast but my console behaviour is very different.
My Chrome console(used in the screecast) produces this result.
albums = new Albums()
child
albums.fetch()
Object
albums.models()
TypeError: Property 'models' of object [object Object] is not a function
the screencast console looks like this
albums = new Albums()
inherits.child
albums.fetch()
inherits.child
albums.models()
[ inherits.child, inherits.child ]
I’m totally lost as to where this is falling apart. Is it my code(see Below),my browser or something else?
(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)
Your code is fine, the example/screencast have errors or is using older backbonejs implementation and is using older chrome thus the child vs inherits.child output.
Backbone.Collectionmodels method – it’s a property on a model instance and should be accessed byalbums.modelsrather thenalbums.models()