简体   繁体   English

灰烬:未触发视图的click事件

[英]Ember: click event on a view not firing

I have a list of race-item s, each rendered as a view: 我有一个race-item的列表,每个都呈现为一个视图:

        <ul>
            {{#each race in controller}}
                {{render "race-item" race class="race-item"}}
                {{else}}
                <li id="imagine">{{#link-to 'races.create'}}Imagine Faster{{/link-to}}</li>
            {{/each}}
        </ul>

In the view class, I want use a click event to toggle a property: 在视图类中,我想使用click事件来切换属性:

App.RaceItemView = Ember.View.extend({
    tagName: 'li',
    templateName: 'raceItem',
    classNameBindings: ['isSelected:selected'],
    isSelected: false,
    click: function(e) {
        this.set('isSelected', true);
    }
});

This click event will not fire, it seems, unless it is a DOM element that normally handles clicks ( a , button , etc.). 此单击事件将不火,现在看来,除非是通常用于处理点击(DOM元素abutton等)。

How can I get a click to fire on my li ? 我该如何点击才能触发我的li I wrap everything in an anchor because there are child elements with their own events and actions. 我将所有内容都包裹在一个锚点中,因为有些子元素具有自己的事件和动作。

Followup question: It seems like even if I get my click working, it will only set the current view's isSelected property to true, but not toggle the rest of the views properties to false. 后续问题:似乎即使我单击我的鼠标,它也只会将当前视图的isSelected属性设置为true,而不会将其余视图属性切换为false。 Tips on accomplishing that? 完成此操作的提示?

Thanks. 谢谢。 I'm dyin' here. 我在这里死。

Click events should work for all elements, including li. 点击事件应适用于所有元素,包括li。 There are a number of ways to achieve what you are looking for: one is to send an event to the controller: 有多种方法可以实现您想要的目标:一种是将事件发送到控制器:

App.ItemView = Ember.View.extend({

  tagName: 'li',
  templateName: 'item-view',
  classNameBindings: ['isSelected:selected'],
  isSelected: false,
  click: function(e) {
    this.set('isSelected',true);
    this.get('controller').send('itemSelected', this);
  }

});

Here is a working example: http://jsbin.com/tolod/2/edit?html,js,output 这是一个工作示例: http : //jsbin.com/tolod/2/edit?html,js,输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM