JavascriptMVC 绑定不起作用

发布于 2024-11-08 15:58:35 字数 904 浏览 4 评论 0原文

我只想做一个简单的按钮单击。但点击时没有任何反应 ;-(

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云淡风轻 2024-11-15 15:58:35

onDocument: true 意味着您已将控制器声明为文档控制器,并且文档控制器在每个选择器之前添加隐式“#CONTROLLERNAME”。

因此,您的事件处理程序定义实际上是:

'#extra click': function( el ) {
    alert("click");
}

您的页面上可能没有 #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:

'#extra click': function( el ) {
    alert("click");
}

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.

小傻瓜 2024-11-15 15:58:35

给你的链接一个id或类,例如

<a href="javascript:;" id="myLink">Click</a>

然后在你的控制器中,链接注册处理程序应该看起来像

'#myLink click': function(){
    //...
 }

这样应该可以工作。另请注意,文档控制器已被弃用。您应该创建一个普通的普通控制器,然后将其附加到某个元素。

Give your link an id or class, for instance

<a href="javascript:;" id="myLink">Click</a>

Then in your controller, the link registration handler should look like

'#myLink click': function(){
    //...
 }

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.

黒涩兲箜 2024-11-15 15:58:35

还有一些“MainController”不会在每个选择器之前添加#controllername。我相信这就是您想要实现的目标。您需要将此行添加到控制器定义的静态部分:

hasActiveElement : document.activeElement || false

这将使您的控制器看起来像:

$.Controller.extend("MainController",{
  hasActiveElement : document.activeElement || false
},{
  focus : function(el){
     if(!this.Class.hasActiveElement)
         document.activeElement = el[0] //tracks active element
  }
})

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:

hasActiveElement : document.activeElement || false

That would make your controller look like:

$.Controller.extend("MainController",{
  hasActiveElement : document.activeElement || false
},{
  focus : function(el){
     if(!this.Class.hasActiveElement)
         document.activeElement = el[0] //tracks active element
  }
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文