为什么我的 CoffeeScript/backbone.js 事件没有触发?
我正在尝试熟悉 CoffeeScript 和backbone.js,但我一定错过了一些东西。
此 CoffeeScript:
MyView = Backbone.View.extend
events: {
"click" : "testHandler"
}
testHandler: ->
console.log "click handled"
return false
view = new MyView {el: $('#test_container')}
view.render()
生成以下 JavaScript:
(function() {
var MyView, view;
MyView = Backbone.View.extend({
events: {
"click": "testHandler"
},
testHandler: function() {
console.log("click handled");
return false;
}
});
view = new MyView({
el: $('#test_container')
});
view.render;
}).call(this);
但是当我单击 test_container
时,click
事件不会触发 testHandler
。
如果我将输出 JavaScript 更改为:
$(function() {
var MyView, view;
MyView = Backbone.View.extend({
events: {
"click": "testHandler"
},
testHandler: function() {
console.log("click handled");
return false;
}
});
view = new MyView({
el: $('#test_container')
});
view.render;
});
删除 call(this)
并附加 $
,一切都会按预期工作。我缺少什么?
I'm trying to familiarize myself with CoffeeScript and backbone.js, and I must be missing something.
This CoffeeScript:
MyView = Backbone.View.extend
events: {
"click" : "testHandler"
}
testHandler: ->
console.log "click handled"
return false
view = new MyView {el: $('#test_container')}
view.render()
Generates the following JavaScript:
(function() {
var MyView, view;
MyView = Backbone.View.extend({
events: {
"click": "testHandler"
},
testHandler: function() {
console.log("click handled");
return false;
}
});
view = new MyView({
el: $('#test_container')
});
view.render;
}).call(this);
But the click
event does not fire testHandler
when I click in test_container
.
If I change the output JavaScript to:
$(function() {
var MyView, view;
MyView = Backbone.View.extend({
events: {
"click": "testHandler"
},
testHandler: function() {
console.log("click handled");
return false;
}
});
view = new MyView({
el: $('#test_container')
});
view.render;
});
Removing the call(this)
and appending the $
, everything works as expected. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只是一种在指定接收者时立即调用匿名函数的方法。它的工作方式基本上与:
$(function () {})
相同,至少在 jQuery 中是这样,它是在 DOM 树完全构建后运行给定函数的简写。看来这是您的 Backbone.View.extend 函数正常工作的必要条件。
is just a way to immediately invoke an anonymous function while specifying a receiver. It works basically this same way as:
$(function () {})
, at least in jQuery, is shorthand forwhich runs the given function when the DOM tree has been fully constructed. It seems like this is the necessary condition for your
Backbone.View.extend
function to work properly.要将 CoffeeScript 和 jQuery 一起用于应用程序脚本,请将代码放入此类模板中:
For using CoffeeScript and jQuery together for application scripts, put your code in this sort of template:
尝试使用 CoffeeScript 类声明语法,例如:
Try using CoffeeScript class declaration syntax, e.g.:
当视图或至少 view.el 动态生成时会发生什么?您可以调用 view.delegateEvents() 方法来手动设置事件处理程序。
这是类似的咖啡脚本,用于在 ParentView 中渲染 ChildView,然后委托 childView 的事件。
What happens when the view, or at least view.el is dynamically generated? You can call the view.delegateEvents() method to manually set the eventhandlers.
Here's similar coffeescript for rendering a ChildView in a ParentView then delegating the childView's events.