How to use the can/util/can.event function in can

To help you get started, we’ve selected a few can examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github canjs / canjs / event / namespace / namespace.js View on Github external
//
// ```
// object.bind("change.orange", function() {});
// object.bind("click.orange", function() {});
//
// // This unbinds all events using the "orange" namespace.
// object.unbind(".orange");
// ```
var can = require('can/util/can');
require('can/event/event');

// ## can.event.addEvent
//
// Adds an event listener (with namespacing support included).
var addEvent = can.addEvent;
can.addEvent = can.event.addEvent = can.event.on = can.event.bind = function(event, fn) {
	// Bypass namespaces for Maps
	// Otherwise it conflicts with map attribute binding.
	if (can.Map && this instanceof can.Map) {
		return addEvent.call(this, event, fn);
	}

	// Split the namespaces out
	if (event && event.indexOf('.') > -1) {
		var namespaces = event.split('.');
		// The event name is the first item in the string
		event = namespaces[0];
	}

	// Add the listener using the original addEvent
	addEvent.call(this, event, fn);
github canjs / canjs / event / delegate / delegate.js View on Github external
/**
 * @function can.event.delegate.delegate
 * @parent can.event.delegate
 * @plugin can/event/delegate
 * @signature `obj.delegate( selector, event, handler )`
 *
 * Adds a delegate event listener.
 *
 * @param {String} selector A selector to match against the stack of propagated objects.
 *                          The names used are based on the names of the constructors of
 *													the original objects.
 * @param {Object|String} event The event name or object to listen to.
 * @param {Function} handler The handler to call when the event occurs.
 * @return {Object} this
 */
can.event.delegate = function(selector, event, handler) {
	// Split the selector into parts that can be verified
	var parts = selector && selector.split(/\s+/),
		// Implement the custom delegation handler
		// This is used to verify the selector prior to executing the original handler
		delegate = function(ev) {
			// Verify descendants against the selector
			// These descendants are tracked in the `can/event/propagate` plugin
			for (var i = 0, j = 0, descendant; j < parts.length && (descendant = ev.descendants[i]); i++) {
				// A descendant name is considered valid if it matches the `shortName` or `_shortName`
				// properties on the constructor. Generally, this assumes that `can.Construct` or a
				// similar can-based class is used (which defines those properties by default).
				if (descendant.constructor && (parts[j] === descendant.constructor._shortName || parts[j] === descendant.constructor.shortName)) {
					j++;
				}
			}
github canjs / canjs / event / namespace / namespace.js View on Github external
// Inject namespaces if applicable
	if (namespaces && namespaces.length > 1) {
		var events = this.__bindEvents[event];
		// Assign the namespaces property, including the array of all namespaces
		events[events.length-1].namespaces = namespaces.slice(1);
	}

	return this;
};

// ## can.event.removeEvent
//
// Removes an event listener (with namespacing support included).
var removeEvent = can.removeEvent;
can.removeEvent = can.event.removeEvent = can.event.off = can.event.unbind = function(event, fn, __validate) {
	// Bypass namespaces for Maps
	// Otherwise it conflicts with map attribute binding.
	if (can.Map && this instanceof can.Map) {
		return removeEvent.call(this, event, fn);
	}

	// Split the namespaces out
	if (event && event.indexOf('.') > -1) {
		var namespaces = event.split('.');
		// The event name is the first item in the string.
		// Also, remove this item from the namespace array for future processing.
		event = namespaces.splice(0,1)[0];
	}

	// Handle namespace-only (no event name).
	if (!event && namespaces && namespaces.length > 0) {
github canjs / canjs / event / propagate / propagate.js View on Github external
//
// // This will dispatch on both child **and** parent.
// child.dispatch("action");
// ```
var can = require('can/util/can');
require('can/event/event');
// ## can.event.dispatch
//
// Adds propagation of events.
// Typically this will be integrated using `can.extend` on an object.
//
// ```
// can.extend(Class.prototype, can.event, { propagate: 'prop' })
// ```
var dispatch = can.dispatch;
can.dispatch = can.event.dispatch = can.event.trigger = function (event, args) {
	var propagate = this.propagate || false;

	// Inject propagation into event, when applicable
	if (typeof event.isPropagationStopped === 'undefined') {
		// Initialize the event into an object
		if (typeof event === 'string') {
			event = {
				type: event
			};
		}

		// Add extra event properties
		// This could be done with can.simpleExtend, but this avoids extra logic execution
		var stop = false,
			prevent = false;
github canjs / canjs / util / zepto / zepto.js View on Github external
// zepto.js
// ---------
// _Zepto node list._

var oldEach = can.each;
// Extend what you can out of Zepto.
var oldPlain = can.isPlainObject;
$.extend(can, Zepto);
can.inArray = function(el, arr) {
	return !arr ? -1 : $.inArray.apply($, arguments);
};
can.isPlainObject = oldPlain;
can.each = oldEach;
can.attr = attr;
can.event = event;
function likeArray(obj) { return typeof obj.length === 'number'; }
can.map = function(elements, callback) {
	var value, values = [], i, key;
	if (likeArray(elements)) {
		for ( i = 0; i < elements.length; i++) {
			value = callback(elements[i], i);
			if (value != null) {
				values.push(value);
			}
		}
	} else {
		for (key in elements) {
			value = callback(elements[key], key);
			if (value != null) {
				values.push(value);
			}
github canjs / canjs / event / propagate / propagate.js View on Github external
event.currentTarget = this;
	}

	// Call original dispatch
	dispatch.call(this, event, args);

	// Call propagated events
	if (propagate && !event.isPropagationStopped() && this[propagate]) {
		// Call the propagated can.dispatch (otherwise it'll only propagate one level)
		can.dispatch.call(this[propagate], event, args);
	}

	return event;
};

module.exports = exports = can.event;
github canjs / canjs / event / delegate / delegate.js View on Github external
//
// Removes a delegate event listener.
/**
 * @function can.event.delegate.undelegate
 * @parent can.event.delegate
 * @plugin can/event/delegate
 * @signature `object.undelegate( selector, event, handler )`
 *
 * Removes a delegate event listener.
 *
 * @param {String} selector A selector to match against the stack of propagated objects.
 * @param {Object|String} event The event name or object to listen to.
 * @param {Function} handler The handler to call when the event occurs.
 * @return {Object} this
 */
can.event.undelegate = function(selector, event, fn) {
	var isFunction = typeof fn === 'function';
	// Attempt to remove the event, with a custom verification function
	return can.removeEvent.call(this, event, fn, function(ev) {
		// This allows us to check for the cached selector and handler
		// as part of the verification. This is necessary because the user isn't
		// going to have access to the custom handler generated for delegation.
		return isFunction && (ev.handler === fn || (ev.handler.handler === fn && ev.handler.selector === selector)) || !isFunction && ev.cid === fn;
	});
};