can-EVENT
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.
Parameters
-
EVENT
{String}A event name like
clickorkeyup. If you are using jQuery, you can listen to jQuery special events too. -
methodKey
{key}A named key value in the current scope. The value should be a function.
-
argKey
{key}Optional VariableKey values that will be passed as arguments to the
methodKeyfunction value. Key values can be read from the scope, or literals like"foo",1, etc.The following key values are also supported:
@element- The can.$ wrapped element the event happened upon.@event- The event object.@viewModel- If the element is a can.Component, the component's viewModel.@context- The current context.@scope- The current can.view.Scope.
If no
argKeys orhashKeys are provided, the methodKey will be called with the@context,@elementand@eventas arguments. -
hashName
{String}A property name that gets added to the
hashargument. The hash argument is the last value passed to the function specified bymethodKey. -
hashKey
{key}Specifies value that is added to the
hashargument for ahashNamevalue.hashKeysupports the same key values asargKey.
Use
By adding
can-EVENT='methodKey'to an element, the function pointed to bymethodKeyis bound to the element'sEVENTevent. The function can be passed any number of arguments from the surrounding scope, orname=valueattributes for named arguments. Direct arguments will be provided to the handler in the order they were given, exceptname=valuearguments, which will all be given as part of ahashargument inserted after all direct arguments.If no
argKeys orhashKeys are provided, the methodKey will be called with the@content,@elementand@eventas arguments.The following uses
can-click={items.splice @index 1}to remove a item fromitemswhen that item is clicked on.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.can-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:
The above template snippet would cause the save method (in the Mustache scope) whenever the user hits the enter key on this input.