backbone.js 简单视图事件未触发
有人可以告诉我为什么以下不起作用。我希望单击链接时会触发警报
<body>
<a class="newnote" href="#">Add new note</a>
</body>
<script>
var note = Backbone.Model.extend({});
var notes = Backbone.Collection.extend({
model: note,
url: '<?= $this->createUrl('/filenotes/api'); ?>'
});
var note_view = Backbone.View.extend({
el: 'table',
});
var Appview = Backbone.View.extend({
el: $('body'),
events: {
"a click" : "showForm"
},
initialize: function(){
//alert('hi');
},
showForm: function(){
alert('hi');
}
});
var v = new Appview();
</script>
这里有一个 jsfiddle http://jsfiddle.net /尼尔查尔顿/yj5Pz/1/
Can someone tell me why the following doesnt work please. I am expecting the alert to fire when I click the link
<body>
<a class="newnote" href="#">Add new note</a>
</body>
<script>
var note = Backbone.Model.extend({});
var notes = Backbone.Collection.extend({
model: note,
url: '<?= $this->createUrl('/filenotes/api'); ?>'
});
var note_view = Backbone.View.extend({
el: 'table',
});
var Appview = Backbone.View.extend({
el: $('body'),
events: {
"a click" : "showForm"
},
initialize: function(){
//alert('hi');
},
showForm: function(){
alert('hi');
}
});
var v = new Appview();
</script>
There is a jsfiddle here http://jsfiddle.net/neilcharlton/yj5Pz/1/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
$("body")
选择器在加载 DOM 之前触发,因此它返回空并且永远不会绑定到链接。主干对象定义是使用对象文字定义的,这意味着它们在定义时立即进行评估,而不是在实例化模型时进行评估。
这:
在功能上与此相同:
由于您有一个
$("body")
选择器作为el
的值,因此一旦 View 配置被执行,它就会被评估已解析,意味着 DOM 尚未加载,不会返回任何内容。有很多选项可以解决这个问题……所有这些都涉及将选择器的评估延迟到 DOM 加载之后。
我个人最喜欢的是在 DOM 加载后,在实例化时将选择器传递到视图中:
另外 - 就像 Skylar 所说,你的事件被声明为错误的。
这是一个工作的 JSFiddle: http://jsfiddle.net/yj5Pz/5/
Your
$("body")
selector is firing before your DOM is loaded, so it's returning empty and never binding to the link.Backbone object definitions are defined with object literals, which means they get evaluated immediately on definition, not when you instantiate the model.
This:
Is functionally the same as this:
Since you have a
$("body")
selector as the value forel
, it gets evaluated as soon as the View configuration is parsed, meaning the DOM has not yet loaded and nothing will be returned.There are a number of options for solving this... all of which involve delaying the evaluation of the selector until after the DOM is loaded.
My personal favorite is to pass the selector in to the view at instantiation, after the DOM has loaded:
Also - like Skylar said, your event was declared wrong.
Here's a working JSFiddle: http://jsfiddle.net/yj5Pz/5/
对于初学者来说,事件对象的语法如下:
For starters, the events object is of this syntax:
除了 Derick 的回答之外,请注意 DOM 元素的范围,我试图在一个元素上触发一个简单的单击事件,但我的 View.el 引用了一个非直接父级,然后由于某种原因(可能是递归错误)它不是'工作。当我将 el 更改为“body”CSS 选择器时,一切顺利。
Besides Derick's answer, take care about the scope of DOM elements, I was trying to trigger a simple click event on an element but my View.el was referencing an non direct parent , then it for some reason (maybe a recursion bug) wasn't working. When I changed the el to "body" css selector everything went fine.