简体   繁体   English

通过ember.js动作传递jQuery $(this)?

[英]Pass jQuery $(this) via ember.js action?

So in my code i have a simple faq that looks like below. 所以在我的代码中,我有一个简单的常见问题,如下所示。

<section class="faq-section">
    <label class="question">Q:This is a Questions?</label>           
    <p class="answer">This is a an answer </p> 
</section>
<section class="faq-section">
    <label class="question">Q:This is a Questions?</label>           
    <p class="answer">This is an answer</p> 
</section>

I want to be able to have a user click a question and reveal an answer. 我希望能够让用户单击问题并显示答案。 In regular jQuery i would do something like this: 在普通的jQuery中,我会做这样的事情:

$( ".question" ).click().next( ".answer" ).toggle('slow');

How would i accomplish something like this using an ember route and action 我如何使用余烬路线和行动来完成类似的任务

<section class="faq-section">
    <label class="question" {{action openQuestion}}>Q:This is a Questions?</label>           
    <p class="answer">This is an answer</p> 
</section>

Ember generally wants you to avoid interaction with the DOM directly, so I don't think there's built in support for this type of usage. Ember通常希望您避免直接与DOM交互,因此我认为没有内置支持这种用法的方法。 But I have a similar use case, and I do something like this: 但是我有一个类似的用例,我做这样的事情:

<section class="faq-section">
    <label class="question" {{action 'openQuestion' questionId}}>blah</label>
    <p class="answer" {{bind-attr data-question-id=questionId}}>blah</p>
</section>

Then, in your controller: 然后,在您的控制器中:

actions: {
    openQuestion: function(questionId) {
        this.$('.answer[data-question-id=' + questionId + ']').toggle('slow');
    }
}

This is assuming that you can come up with a unique questionId for each question, but that shouldn't be too difficult. 这是假设你能想出一个独特的questionId对于每个问题,但应该不会太困难。

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

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