ember.js ReferenceError:在firefox中没有定义事件

时间:2013-11-11 02:01:36

标签: javascript ember.js

我想访问ember动作中的事件对象来检查目标元素。这适用于Chrome和Safari,但不适用于Firefox 25.0。

我甚至没有收到错误消息。如何在ember操作中访问事件对象,或者有一种实现它的方法吗?

如何重现:

  1. 打开我的小提琴
  2. 点击div或“点击我”链接,该链接应打开一个提示框
  3. 在chrome和firefox中测试这些
  4. HTML

      <script type="text/x-handlebars" data-template-name="application">
          <h1>Click the link in the following div</h1>
          {{outlet}}
      </script>
      <script type="text/x-handlebars" data-template-name="index">
          {{test-a}}
      </script>
      <script type="text/x-handlebars" data-template-name="components/test-a">
        <div class="index" {{action 'edit' on="click"}}>
            <a href="http://www.example.com" target="_blank">Click me</a>
        </div>
      </script>
    

    咖啡

    App = Ember.Application.create({});
    
    App.TestAComponent = Ember.Component.extend
        actions:
            edit: ->
                if event.toElement.nodeName.toUpperCase() == 'A'
                    return true # should bubble to the window but is a component
                # do something for editing here
                alert('div clicked')
                false
    

    CSS

    h1 { font-size: 1.6em; padding-bottom: 10px; }
    h2 { font-size: 1.4em; }
    ul { padding: 15px; font-size: 1.4em; color: green; }
    .index { background-color: #666; padding: 20px; }
    

    http://jsfiddle.net/mszrnyi/3REEj/2/

1 个答案:

答案 0 :(得分:0)

有些浏览器会在window.event中创建一个全局事件对象,但这不是跨浏览器。

使用动作处理程序,您将无法正常工作。因为动作是处理浏览器事件的最佳方式。因此,没有传递任何事件对象,并且当{{action}}被触发event.preventDefault()时,会执行,因此您的链接将无法打开新窗口。

您只需使用click即可。这是处理浏览器事件的低级方法,因此您将在第一个参数中获取事件对象。并且基于布尔返回的泡沫预期行为:

 App.TestAComponent = Ember.Component.extend    
    click: (event) ->                
        if event.toElement.nodeName.toUpperCase() == 'A'
            alert('link clicked')
            return true # should bubble to the window but is a component
        # do something for editing here
        alert('div clicked')       
        false

此外,需要从{{action 'edit' on="click"}}模板中删除components/test-a。否则,event.preventDefault()将被执行。

  <script type="text/x-handlebars" data-template-name="components/test-a">
    <div class="index">
        <a href="http://www.example.com" target="_blank">Click me</a>
    </div>
  </script>

这是更新的小提琴http://jsfiddle.net/8Ephq/