In my Followers collection inside my backbone app i have the following
window.Curate.Collections.Following = Backbone.Collection.extend({
model: Curate.Models.User,
initialize: function (models, options) {
this.add(models);
this.user_id = options.user_id;
},
url: function () {
return '/api/users/' + this.user_id + '/following';
},
parse: function(response){
this.page_number = parseInt(response.page_number);
this.total_pages = parseInt(response.total_pages);
return response.users;
}
});
window.Curate.Collections.following = new Curate.Collections.Following();
Curate.Collections.following.fetch({
data: { page: 1 }
});
whats confusing me is that inside the initialize object the user_id in options.user_id is throwing an error
Uncaught TypeError: Cannot read property 'user_id' of undefined
now this does what i want it to do which is to get the user_id so i can put it into the api url but this error occurs which in return wont allow me to push to heroku.
Any idea of what going on here? Thanks
The problem is that you didn't pass any arguments to your collection, actually you don't pass any options
object
new Curate.Collections.Following();
It should be something like this and includes options
object with user_id
new Curate.Collections.Following([{}, {}], { user_id: 123 });
P.S. this line inside initialize
method of Backbone.Collection
is not necessary
this.add(models);
because Backbone automatically resets collection with passed in array of models in constructor after initialize.