In running this on index.html, I get the following error: “Uncaught SyntaxError: Unexpected token :”, referring to
events: {
"click #add-friend": "showPrompt",
},
It specifically refers to the “:” here “click #add-friend”: “showPrompt”
More context below. Any advice would be appreciated.
(function ($) {
Friend = Backbone.Model.extend({
// create a model to to hold friend attribute
name: null
});
Friends = Backbone.Collection.extend({
// this is our friends collection and holds out Friend models
initialize: function(models, options) {
this.bind("add", options.view.addFriendLi);
// listens for "add" and calls a view function if so
}
});
AppView = Backbone.View.extend({
el: $("body"),
initialize: function () {
this.friends = new Friends(null, {view: this});
// creates a new friends collection when the view is initialized
// pass it a reference to the view to create a connection between the two
events: {
"click #add-friend": "showPrompt"
},
showPrompt: function () {
var friend_name = prompt("Who is your friend?");
var friend_model = new Friend({name:friend_name});
// adds a new friend model to out Friend collection
this.friends.add(friend_model);
},
addFriendLi: function (model) {
// the parameter passed is a reference to the model that was added
$("#friends_list").append("<li>" + model.get('name') + "</li>");
}
});
var appview = new AppView;
})(jQuery);
You’ve got an extra comma at the end:
You are also missing a closing
}at the end of the initialize method: