-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from canjs/docs
improved docs #46
- Loading branch information
Showing
7 changed files
with
183 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
@module {can-dom-events/EventRegistry} can-dom-events | ||
@parent can-dom-utilities | ||
@collection can-infrastructure | ||
@package ../package.json | ||
@group can-dom-events.static 0 static | ||
@group can-dom-events.types 1 types | ||
|
||
@description Listen to DOM events and special events, and register | ||
special events. | ||
|
||
@type {can-dom-events/EventRegistry} | ||
The `can-dom-events` module exports the _global_ event registry. Use | ||
it to listen to DOM events on HTML elements. Any custom event | ||
types added to this registry are available to every other part of CanJS. | ||
|
||
|
||
@body | ||
|
||
## Use | ||
|
||
The following listens to a 'change' event on an element: | ||
|
||
```js | ||
var domEvents = require("can-dom-events"); | ||
var input = document.createElement('input'); | ||
|
||
function onChange(event) { | ||
console.log('Input value changed to:', event.target.value); | ||
} | ||
|
||
domEvents.addEventListener(input, 'change', onChange); | ||
``` | ||
|
||
Use [can-dom-events.dispatch] to fire custom events: | ||
```js | ||
domEvents.dispatch(input, 'change'); | ||
``` | ||
|
||
Use [can-dom-events.addDelegateListener] to listen to an event for all elements that | ||
match a selector within a root element: | ||
|
||
```js | ||
domEvents.addDelegateListener(document.body,"click", "a", function(event){ | ||
event.preventDefault(); | ||
}); | ||
``` | ||
|
||
Finally, you can create your own custom events and add them to | ||
the global event registry. First, create an [can-dom-events/EventDefinition] | ||
object. For example, the following might implement | ||
an "escape" event that listens to when a user presses the `Escape` key: | ||
|
||
|
||
```js | ||
var handlerMap = new WeakMap(); | ||
var escapeEventDefinition = { | ||
defaultEventType: 'escape', | ||
addEventListener: function (target, eventType, handler) { | ||
var keyHandler = function (event) { | ||
if (event.keyCode === 27 || event.key === 'Escape') { | ||
return handler.apply(this, arguments); | ||
} | ||
}; | ||
handlerMap.set(handler, keyHandler) | ||
this.addEventListener(target, baseEventType, keyHandler); | ||
}, | ||
removeEventListener: function (target, eventType, handler) { | ||
this.removeEventListener(target, baseEventType, handlerMap.get(handler)); | ||
} | ||
} | ||
``` | ||
|
||
Then you can add this custom event to the registry and listen to that event: | ||
|
||
```js | ||
import domEvents from "can-dom-events"; | ||
|
||
domEvents.addEvent(escapeEventDefinition); | ||
|
||
var input = document.querySelector("[name=search]"); | ||
domEvents.addEventListener(input, "escape", function(){ | ||
|
||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
@typedef {Object} can-dom-events.DomEventTarget DomEventTarget | ||
@parent can-dom-events.types | ||
|
||
@description An object which can have DOM Events registered on it. | ||
This is a Window, Document, or HTMLElement. | ||
|
||
@signature `Window|Document|HTMLElement` | ||
|
||
@type {Window} | ||
@type {Document} | ||
@type {HTMLElement} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
@typedef {Object} can-dom-events/EventDefinition EventDefinition | ||
@description Definition of a custom event that may be added to an event registry. | ||
@parent can-dom-events.types | ||
@type {Object} | ||
@option {String} [defaultEventType] | ||
The default event type of the event. | ||
|
||
@option {function} [addEventListener] | ||
The function to add the listener to the target. | ||
@param {DomEventTarget} target The target to which to add the listener. | ||
@param {String} eventType The event type which should be used to register the listener. | ||
@param {*} eventArgs The arguments should to configure the listener behavior. | ||
|
||
@option {function} [removeEventListener] | ||
The function to remove the listener from the target. | ||
@param {DomEventTarget} target The target to which to add the listener. | ||
@param {String} eventType The event type which should be used to register the listener. | ||
@param {*} eventArgs The arguments should to configure the listener behavior. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@typedef {Object} can-dom-events/EventRegistry EventRegistry | ||
@parent can-dom-events.types | ||
@hide | ||
@description Blah |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters