Complimentary template helpers to be used with the {{on}}
element modifierspecified by RFC #471 "{{on}}
modifier".
ember install ember-event-helpers
If you are below Ember 3.10, you'll also want to install the{{on}}
modifier polyfill:
ember install ember-on-modifier
�� For usage information on{{on}}
itself, refer to the RFC orpolyfill documentation.
Template Helper | Event method |
---|---|
(prevent-default fn) |
event.preventDefault() |
(stop-propagation fn) |
event.stopPropagation() |
(stop-immediate-propagation fn) |
stopImmediatePropagation |
All three template helpers return a function that, when invoked, will call theassociated Event
method on the first argument. The helper themselves also takean optional fn
argument, which is a function that will be called synchronouslyafterwards with all input arguments of the returned function. The return valueof fn
is passed through.
Sounds complicated? Let's see some examples instead!
(prevent-default)
Calls event.preventDefault()
.
Prevent the user agent from performing the default action, like toggling acheckbox, when it is clicked. The event continues to propagate as usual.
import Component from '@ember/component';
import { action } from '@ember/object';
export default class CheckboxesComponent extends Component {
@action
onClick(event: MouseEvent) {
if (event.defaultPrevented) {
console.log('Checkbox will not be toggled.');
} else {
console.log('Checkbox will be toggled.');
}
}
}
�� The@action
decorator is used to bind theonClick
method'sthis
context to the component instance. This is not required here, sincethis
is not accessed, but in order to not break with patterns, we still doit here.
Using the old {{action}}
modifier you wouldexpress the same thing like this:
(stop-propagation)
Calls event.stopPropagation()
.
Stops further propagation of the current event in the capturing phase (down theDOM) and bubbling phase (up the DOM).
import Component from '@ember/component';
import { action } from '@ember/object';
export default class BubbleGumComponent extends Component {
@action
onOuterClick(event: MouseEvent) {
console.log('outer');
}
@action
onInnerClick(event: MouseEvent) {
console.log('inner');
}
}
Clicking .inner-a
will print:
inner
outer
Clicking .inner-b
will only print:
inner
If you enable the capture
event option and use (stop-propagation)
with it, the event propagation will already be stopped in the capture phase("down the DOM").
Clicking .inner
will only print:
outer
(stop-immediate-propagation)
⚠️ Not implemented yet.
Calls stopImmediatePropagation
.
Like stopPropagation
, but additionally even stopping any further listeners onthe current element in the bubbling / capturing phase to be called.
�� Imagine it like this:stopPropagation
only stops further propagationvertically, so further down the DOM (capture phase) or back up the DOM(bubble phase).stopImmediatePropagation
additionally prevents any furtherhorizontal propagation, so any further listeners on the same element willnot be called.
In practice, you will probably never need this helper.
import Component from '@ember/component';
import { action } from '@ember/object';
export default class BubbleGumComponent extends Component {
@action
onOuterClick(event: MouseEvent) {
console.log('outer');
}
@action
onInnerClickA(event: MouseEvent) {
console.log('inner A');
}
@action
onInnerClickB(event: MouseEvent) {
console.log('inner B');
}
}
Clicking the first button prints:
inner A
inner B
The listeners are executed in the order they were registered in. The listener on.outer
is not called, since the first listener uses (stop-propagation)
, sothere is no bubbling.
Clicking the second button prints:
inner A
Since the first listener uses (stop-immediate-propagation)
, the secondlistener is not called. The .outer
listener is also not called.
If you want to curry the function call / partially apply arguments, you can doso using the {{fn}}
helper:
You can nest the helpers (recommended):
Or register additional "void" helpers, since the fn
argument is optional:
Alternatively you could even use (queue)
from ember-composable-helpers
.
event.target.value
With the {{action}}
modifier / helper, you used to be able to conveniently accessevent.target.value
(or any other property thereof), like so:
You can still easily do this with (pick)
from ember-composable-helpers
.
Type: DOMEvent MooTools的DOMEvent方法。 DOMEvent Method: constructor 语法: new DOMEvent([event[, win]]); 参数: event - (event, required)HTMLEvent对象。 win - (window, optional: defaults to window)事件的上下文。 属性: pag
组件 触发字符 mui.on(事件绑定) mmon mui.off(事件取消) mmoff mui.trigger()(事件触发) mtrigger mui.fire()(自定义事件) mfire document.getElementById() dg document.querySelector() ds document.querySelector().addEventListener()
Swoole扩展还提供了直接操作底层epoll/kqueue事件循环的接口。可将其他扩展创建的socket,PHP代码中stream/socket扩展创建的socket等加入到Swoole的EventLoop中, 否则第三方的$fd如果是同步IO会导致Swoole的EventLoop得不到执行,参考案例。 !> Event模块比较底层,是epoll的初级封装,使用者最好有IO多路复用编程经验。 事
描述:描述一个事件。 语法 @event <className>#[event:]<eventName> 概述 描述一个事件。@event标签允许您描述一个可触发的事件,一个典型的事件是由对象定义的一组属性来表示。 标签来定义事件的具体类型,您可以使用@fires标记,以表明这个种方法可以触发该事件。你还可以使用@listens标签,以指示表明用这个表示来侦听该事件。 JSDoc自动预先考虑命名空
Event::fire('foo.bar', array($bar)); // 注册一个事件监听器. // void listen(string|array $events, mixed $listener, int $priority) Event::listen('App\Events\UserSignup', function($bar){}); Event::listen('foo.*',
此绑定用于侦听特定的DOM事件以及基于它的与处理程序函数关联的调用。 Syntax event: <{DOM-event: handler-function}> Parameters 参数包含一个JavaScript对象,包含将被侦听的DOM事件和一个需要根据该事件调用的处理函数。 这个函数可以是任何JavaScript函数,也不一定是ViewModel函数。 Example 让我们看一下下面的