can-event Object
assign(YourClass.prototype, canEvent)
- static
obj.addEventListener(event, handler)
canEvent.addEventListener.call(obj, event, handler)
obj.removeEventListener(event, handler)
canEvent.removeEventListener.call(obj, event, handler)
obj.dispatch(event, args)
canEvent.dispatch.call(obj, event, args)
obj.on(event, handler)
can-event.on.call(obj, event, handler)
obj.off(event, handler)
canEvent.off.call(obj, event, handler)
obj.one(event, handler)
obj.listenTo(other, event, handler)
canEvent.listenTo.call(obj, other, event, handler)
obj.stopListening(other, event, handler)
canEvent.stopListening.call(obj, other, event, handler)
obj.bind(event, handler)
canEvent.bind.call(obj, event, handler)
obj.unbind(event, handler)
canEvent.unbind.call(obj, event, handler)
obj.delegate(selector, event, handler)
canEvent.delegate.call(obj, selector, event, handler)
obj.undelegate(selector, event, handler)
canEvent.undelegate.call(obj, selector, event, handler)
- static
Add event functionality into your objects.
The canEvent
object provides a number of methods for handling events in objects. This functionality is best used by mixing the canEvent
object into an object or prototype. However, event listeners can still be used even on objects that don't include canEvent
.
All methods provided by canEvent
assume that they are mixed into an object -- this
should be the object dispatching the events.
Adds event functionality to YourClass
objects. This can also be applied to normal objects: assign(someObject, canEvent)
.
The assign
function can be any function that assigns additional properties on an object such as Object.assign or lodash's _.assign or [can-util/js/assign/assign].
var assign = require("can-util/js/assign/assign");
var canEvent = require("can-event");
function Thing(){
}
assign(Thing.prototype, canEvent);
// Can not apply event listeners
var thing = new Thing();
thing.addEventListener("prop", function(){ ... });
Add a basic event listener to an object.
var canEvent = require("can-event");
var obj = {};
Object.assign(obj, canEvent);
obj.addEventListener("foo", function(){ ... });
- event
{String}
: The name of the event to listen for. - handler
{function}
: The handler that will be executed to handle the event.
- returns
{Object}
: this
This syntax can be used for objects that don't include the canEvent
mixin.
Removes a basic event listener from an object.
- event
{String}
: The name of the event to listen for. - handler
{function}
: The handler that will be executed to handle the event. - __validate
{function}
: An extra function that can validate an event handler as a match. This is an internal parameter and only used for can-event plugins.
- returns
{Object}
: this
This syntax can be used for objects that don't include the can-event mixin.
Dispatches/triggers a basic event on an object.
var canEvent = require("can-event");
var obj = {};
Object.assign(obj, canEvent);
obj.addEventListener("foo", function(){
console.log("FOO BAR!");
});
obj.dispatch("foo"); // Causes it to log FOO BAR
- event
{String|Object}
: The event to dispatch - args
{Array}
: Additional arguments to pass to event handlers
- returns
{Object}
: event The resulting event object
This syntax can be used for objects that don't include the can.event
mixin.
Add a basic event listener to an object.
This is an alias of addEventListener.
This syntax can be used for objects that don't include the can-event mixin.
Removes a basic event listener from an object.
This is an alias of removeEventListener.
This syntax can be used for objects that don't include the can-event mixin.
Adds a basic event listener that listens to an event once and only once.
- event
{String}
: The name of the event to listen for. - handler
{function}
: The handler that will be executed to handle the event.
- returns
{Object}
: this
Listens for an event on another object. This is similar to concepts like event namespacing, except that the namespace is the scope of the calling object.
- other
{Object}
: The object to listen for events on. - event
{String}
: The name of the event to listen for. - handler
{function}
: The handler that will be executed to handle the event.
- returns
{Object}
: this
This syntax can be used for objects that don't include the can-event mixin.
Stops listening for an event on another object.
- other
{Object}
: The object to listen for events on. - event
{String}
: The name of the event to listen for. - handler
{function}
: The handler that will be executed to handle the event.
- returns
{Object}
: this
This syntax can be used for objects that don't include the can-event mixin.
Add a basic event listener to an object.
This is an alias of addEventListener.
This syntax can be used for objects that don't include the can-event mixin.
Removes a basic event listener from an object.
This is an alias of removeEventListener.
This syntax can be used for objects that don't include the can-event mixin.
Provides a compatibility layer for adding delegate event listeners. This doesn't actually implement delegates, but rather allows logic that assumes a delegate to still function.
Therefore, this is essentially an alias of addEventListener with the selector ignored.
- selector
{String}
: The ignored selector to use for the delegate. - event
{String}
: The name of the event to listen for. - handler
{function}
: The handler that will be executed to handle the event.
- returns
{Object}
: this
This syntax can be used for objects that don't include the [can.event] mixin.
Provides a compatibility layer for removing delegate event listeners. This doesn't actually implement delegates, but rather allows logic that assumes a delegate to still function.
Therefore, this is essentially an alias of removeEventListener with the selector ignored.
- selector
{String}
: The ignored selector to use for the delegate. - event
{String}
: The name of the event to listen for. - handler
{function}
: The handler that will be executed to handle the event.
- returns
{Object}
: this
This syntax can be used for objects that don't include the can-event mixin.
To make a build of the distributables into dist/
in the cloned repository run
npm install
node build
Tests can run in the browser by opening a webserver and visiting the test.html
page.
Automated tests that run the tests from the command line in Firefox can be run with
npm test