can
Version:
MIT-licensed, client-side, JavaScript framework that makes building rich web applications easy.
81 lines (54 loc) • 3.2 kB
Markdown
-EVENT can-EVENT
can.view.bindings
{2.2} Use [can.view.bindings.event event binding] (`($EVENT)="METHOD()"`) instead.
`(EVENT)='{methodKey [argKey..] [hashName=hashKey...]}'`
Specify a callback function to be called on a particular event. This is a shorthand for `can-EVENT` attributes.
`can-EVENT='{methodKey [argKey..] [hashName=hashKey...]}'`
Specify a callback function to be called on a particular event. You can create your own special event types.
{String} EVENT A event name like `click` or `keyup`. If you are
using jQuery, you can listen to jQuery special events too.
{can.stache.key} methodKey A named key value in the current scope. The value
should be a function.
{can.stache.key} [argKey...] Key values that will be passed as
arguments to the `methodKey` function value. Key values can
be read from the scope, or literals like `"foo"`, `1`, etc.
The following key values are also supported:
- ` ` - The [can.$] wrapped element the event happened upon.
- ` ` - The event object.
- ` ` - If the element is a [can.Component], the component's [can.Component::viewModel viewModel].
- ` ` - The current context.
- ` ` - The current [can.view.Scope].
If no `argKey`s or `hashKey`s are provided, the methodKey will be called with the
` `, ` ` and ` ` as arguments.
{String} hashName A property name
that gets added to the `hash` argument. The hash argument is the
last value passed to the function specified by `methodKey`.
{can.stache.key} hashKey Specifies value that is added
to the `hash` argument for a `hashName` value. `hashKey` supports
the same key values as `argKey`.
## Use
By adding `can-EVENT='methodKey'` to an element, the function pointed to
by `methodKey` is bound to the element's `EVENT` event. The function can be
passed any number of arguments from the surrounding scope, or `name=value`
attributes for named arguments. Direct arguments will be provided to the
handler in the order they were given, except `name=value` arguments, which
will all be given as part of a `hash` argument inserted after all direct
arguments.
If no `argKey`s or `hashKey`s are provided, the methodKey will be called with the
` `, ` ` and ` ` as arguments.
The following uses `can-click={items.splice 1}` to remove a
item from `items` when that item is clicked on.
can/view/bindings/doc/can-event-args.html
## Special Event Types
can.view.bindings supports creating special event types
(events that aren't natively triggered by the DOM), which are
bound by adding attributes like `can-SPECIAL='KEY'`. This is
similar to [$.special](http://benalman.com/news/2010/03/jquery-special-events/).
### can-enter / (enter)
can-enter is a special event that calls its handler whenever the enter
key is pressed while focused on the current element. For example:
<input type='text' can-enter='{save}' />
The above template snippet would cause the save method
(in the [can.mustache Mustache] [can.view.Scope scope]) whenever
the user hits the enter key on this input.
can.view.bindings.can