在 index.html 上运行它时,我收到以下错误:“Uncaught SyntaxError: Unexpected token :”,指
events: {
"click #add-friend": "showPrompt",
},
此处特指“:”“点击#add-friend”:“showPrompt”,更多上下文如下。任何意见,将不胜感激。
(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);