JavascriptMVC 绑定不起作用
我只想做一个简单的按钮单击。但点击时没有任何反应 ;-(
Controller:
$.Controller.extend('Cookbook.Controllers.Extra',
{
onDocument: true
},
{
"{window} load": function() {
console.info("loaded");
},
'click': function( el ) {
alert("click");
}
});
Extra.html (入口点)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<a href="#" >click here</a>
<script type='text/javascript' src='../steal/steal.js?extra,development'></script>
</body>
</html>
更新:
此事件正在运行。但点击侦听器不起作用 ;-(
"{window} resize" : function(window, ev){alert("test")}
I just want to do a simple button-click. But nothing happens on click ;-(
Controller:
$.Controller.extend('Cookbook.Controllers.Extra',
{
onDocument: true
},
{
"{window} load": function() {
console.info("loaded");
},
'click': function( el ) {
alert("click");
}
});
Extra.html (entry point)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<a href="#" >click here</a>
<script type='text/javascript' src='../steal/steal.js?extra,development'></script>
</body>
</html>
Update:
This event is working. But not the click-listener ;-(
"{window} resize" : function(window, ev){alert("test")}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
onDocument: true
意味着您已将控制器声明为文档控制器,并且文档控制器在每个选择器之前添加隐式“#CONTROLLERNAME”。因此,您的事件处理程序定义实际上是:
您的页面上可能没有
#extra
元素,因此它永远不会被附加,并且事件永远不会被触发。我自己在开始使用JMVC时就犯过这个错误。文档控制器文档的第一行将解释这一点。
我认为他们可能会在未来的 JMVC 版本中取消文档控制器,而且我发现我不经常使用它们。
The
onDocument: true
means that you've declared your Controller as a Document Controller, and Document Controllers add an implicit '#CONTROLLERNAME' before every selector.So your event handler definition is actually:
You probably don't have an
#extra
element on your page, so it never gets attached, and the event is never fired. I made this mistake myself when I started using JMVC.The first line of the Document Controller documentation will explain this.
I think they may be doing away with Document Controllers in a future JMVC release, and I find that I don't use them too often.
给你的链接一个id或类,例如
然后在你的控制器中,链接注册处理程序应该看起来像
这样应该可以工作。另请注意,文档控制器已被弃用。您应该创建一个普通的普通控制器,然后将其附加到某个元素。
Give your link an id or class, for instance
Then in your controller, the link registration handler should look like
This should work. Also note that document controllers are somehow deprecated. You should create a plain normal controller and then attach it to some element.
还有一些“MainController”不会在每个选择器之前添加#controllername。我相信这就是您想要实现的目标。您需要将此行添加到控制器定义的静态部分:
这将使您的控制器看起来像:
There are also "MainControllers" that do not add #controllername before every selector. I believe that's what you're trying to achieve. You need to add this line to static part of controller definition:
That would make your controller look like: