简体   繁体   English

Ember Component句柄jQuery事件处理程序

[英]Ember Component handle jQuery event handlers

In an Ember app Component, what is the best hook within which to handle jQuery event handlers ? 在Ember应用程序组件中,处理jQuery事件处理程序的最佳方法是什么? I want to handle a keyUp event on an input element. 我想处理输入元素上的keyUp事件。

didInsertElement would be the best place to do anything jquery or dom related operation. didInsertElement将是进行jquerydom相关操作的最佳场所。

didInsertElement() is also a good place to attach event listeners. didInsertElement()也是附加事件侦听器的好地方。 This is particularly useful for custom events or other browser events which do not have a built-in event handler. 这对于自定义事件或其他没有内置事件处理程序的浏览器事件特别有用。

https://guides.emberjs.com/v3.0.0/components/the-component-lifecycle/#toc_integrating-with-third-party-libraries-with-code-didinsertelement-code https://guides.emberjs.com/v3.0.0/components/the-component-lifecycle/#toc_integrating-with-third-party-libraries-with-code-didinsertelement-code

Edit 编辑

We do not need $(document).ready() since document would have already been loaded by that time. 我们不需要$(document).ready()因为那时文档已经被加载了。 You can access the dom element globally or locally. 您可以全局或本地访问dom元素。

You can access globally by using Ember.$() which is a similar to normal query which you can use to select any element on the page. 您可以使用Ember.$()进行全局访问,这与普通查询类似,可用于选择页面上的任何元素。 Even from another component. 即使来自另一个组件。

The better (preferred) approach is to access locally using this.$() which is scoped to component elements only. 更好的(首选)方法是使用this.$()在本地访问, this.$()方法仅适用于组件元素。

For example: 例如:

<h1 class="title">Heading 1</h1>
{{your-component}}

# your-component.hbs
<div> <h2>Component Heading 2</h2></div>

From above example, you can access both and tags inside didInsertElement globally by using Ember.$('h1') and Ember.$('h2') 在上面的示例中,您可以使用Ember.$('h1')Ember.$('h2')全局访问didInsertElement内部的和标签Ember.$('h2')

However if you do this.$('h1') , it will return null as your component template does not have h1 tag and the exiting h1 tag is outside of your component. 但是,如果您执行this.$('h1') ,它将返回null因为您的组件模板没有h1标签,并且退出的h1标签位于您的组件之外。

In nutshell, Ember.$() acts like regular $() and this.$() act like Ember.$('your component root element').find() 简而言之, Ember.$()行为就像常规的$()this.$()行为就像Ember.$('your component root element').find()

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

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