gpt4 book ai didi

javascript - 简单事件backbone.js错误

转载 作者:行者123 更新时间:2023-11-28 13:52:47 25 4
gpt4 key购买 nike

在 index.html 上运行此代码时,出现以下错误:“Uncaught SyntaxError: Unexpected token :”,指的是

events: {
"click #add-friend": "showPrompt",
},

特指这里的“:”“click #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);

最佳答案

最后多了一个逗号:

"click #add-friend": "showPrompt" // remove the comma

您还缺少初始化方法末尾的结束 }:

initialize: function () {
this.friends = new Friends(null, {view: this});
}, // add a "}," here

events: {
"click #add-friend": "showPrompt"
},

关于javascript - 简单事件backbone.js错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9842200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com