简单事件backbone.js错误
Simple events backbone.js error
在index.html上运行此操作时,我得到以下错误:"UnaughtSyntaxError:意外令牌:",指的是
events: {
"click #add-friend": "showPrompt",
},
具体是指":"此处"点击#添加好友":"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"
},
您的"initialize"函数缺少"}"。如果没有这一点,它就会认为令牌"events"正在启动一个新的语句。直到字符串常量后面的冒号都是好的,这在上下文中是语法错误的。
哦,你还需要一个逗号,将"initialize"属性的值与"events"属性分开。
删除属性值后的逗号:
events: {
"click #add-friend": "showPrompt" // <-- comma removed!
},
相关文章:
- Node.js v6.2.0类扩展不是函数错误
- 同样,同样的错误'ahorcado.js:26未捕获类型错误:无法读取属性'beginPath'
- RxJS:在循环中处理错误.js自定义驱动程序
- 如何在快递中抛出 404 错误.js
- 如何捕获服务器错误 JS
- 帆的“500”错误.js“测试项目”
- 快递中的会话未定义错误.js
- jquery.validate 中的错误.js“javaScript 运行时错误:无法获取未定义或空引用的属性'
- 角度动画中的奇怪错误.js
- 如何删除“对象在节点中没有方法'forEach'错误.js”
- 错误 JS 将发布数据发送到控制器 -CodeIgniter
- 使用摩卡在节点上测试错误.js函数
- 为什么 Magento js/varien/form.js IE 中的错误 - js fiddle
- 检测导致冻结等问题的错误 JS
- 三中的错误.js:相机不是三的实例.照相机
- 编译错误js.72.未处理的错误
- PHP停止表单重新加载显示错误Js
- 节点中的套接字挂断错误.js使用 https 时
- 纹理加载错误(JS动画)
- jasmineNodeOpts:执行量角器时出现意外的标识符错误.js