How to use the can/util/util.extend 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 / control / modifier / modifier.js View on Github external
].join(modifier.delim);
			processors[processorName] = function (el, nil, selector, methodName, control) {
				var callback = modifier.modify(mod, can.Control._shifter(control, methodName), control.options);
				control[event] = callback;
				if (!selector) {
					selector = can.trim(selector);
				}
				can.delegate.call(el, selector, event, callback);
				return function () {
					can.undelegate.call(el, selector, event, callback);
				};
			};
		}
	};
// Redefine _isAction to handle new syntax
can.extend(can.Control, {
	modifier: modifier,
	setup: function (el, options) {
		can.each(this.prototype, function (fn, key, prototype) {
			var parts, event, mod;
			if (modifier.hasModifier(key)) {
				// Figure out parts
				parts = key.split(modifier.delim);
				event = parts.shift()
					.split(' ')
					.pop();
				mod = parts.join('');
				if (!(can.trim(key) in processors)) {
					modifier.addProcessor(event, mod);
				}
			}
		});
github canjs / canjs / util / string / deparam / deparam.js View on Github external
var can = require('can/util/util');
require('can/util/string/string');

// ## deparam.js
// `can.deparam`
// _Takes a string of name value pairs and returns a Object literal that represents those params._
var digitTest = /^\d+$/,
	keyBreaker = /([^\[\]]+)|(\[\])/g,
	paramTest = /([^?#]*)(#.*)?$/,
	prep = function (str) {
		return decodeURIComponent(str.replace(/\+/g, ' '));
	};
can.extend(can, {
	deparam: function (params) {
		var data = {}, pairs, lastPart;
		if (params && paramTest.test(params)) {
			pairs = params.split('&');
			can.each(pairs, function (pair) {
				var parts = pair.split('='),
					key = prep(parts.shift()),
					value = prep(parts.join('=')),
					current = data;
				if (key) {
					parts = key.match(keyBreaker);
					for (var j = 0, l = parts.length - 1; j < l; j++) {
						if (!current[parts[j]]) {
							// If what we are pointing to looks like an `array`
							current[parts[j]] = digitTest.test(parts[j + 1]) || parts[j + 1] === '[]' ? [] : {};
						}
github canjs / canjs / map / list / list.js View on Github external
var can = require('can/util/util');
require('can/map/map');
require('can/list/list');
require('can/compute/compute');

can.extend(can.List.prototype, {
	filter: function (callback) {
		// The filtered list
		var filtered = new this.constructor();
		var self = this;
		// Creates the binder for a single element at a given index
		var generator = function (element, index) {
			// The event handler that updates the filtered list
			var binder = function (ev, val) {
				var index = filtered.indexOf(element);
				// Remove it from the list if it exists but the new value is false
				if (!val && index !== -1) {
					filtered.splice(index, 1);
				}
				// Add it to the list if it isn't in there and the new value is true
				if (val && index === -1) {
					filtered.push(element);
github canjs / canjs / map / validations / validations.js View on Github external
});
		},
		validatesNumericalityOf: function (attrNames) {
			validate.call(this, attrNames, function (value) {
				var res = !isNaN(parseFloat(value)) && isFinite(value);
				if (!res) {
					return this.constructor.validationMessages.numericality;
				}
			});
		}
	});
});
/**
 * @prototype
 */
can.extend(can.Map.prototype, {
	errors: function (attrs, newVal) {
		// convert attrs to an array
		if (attrs) {
			attrs = can.isArray(attrs) ? attrs : [attrs];
		}
		var errors = {}, self = this,
			// helper function that adds error messages to errors object
			// attr - the name of the attribute
			// funcs - the validation functions
			addErrors = function (attr, funcs) {
				can.each(funcs, function (func) {
					var res = func.call(self, isTest ? newVal : self.attr(attr));
					if (res) {
						if (!errors[attr]) {
							errors[attr] = [];
						}
github canjs / canjs / map / delegate / delegate.js View on Github external
oldVal,
						from
					]);
				} else if (delegate.event === 'set' && how === 'add') {
					// if we are listening to set, we should also listen to add
					delegate.callback.apply(this.attr(hasMatch), [
						event,
						newVal,
						oldVal,
						from
					]);
				}
			}
		}
	};
can.extend(can.Map.prototype, {
	delegate: function (selector, event, handler) {
		selector = can.trim(selector);
		var delegates = this._observe_delegates || (this._observe_delegates = []),
			attrs = [],
			selectorRegex = /([^\s=,]+)(?:=("[^",]*"|'[^',]*'|[^\s"',]*))?(,?)\s*/g,
			matches;
		// parse each property in the selector
		while ((matches = selectorRegex.exec(selector)) !== null) {
			// we need to do a little doctoring to make up for the quotes.
			if (matches[2] && can.inArray(matches[2].substr(0, 1), [
				'"',
				'\''
			]) >= 0) {
				matches[2] = matches[2].substr(1, -1);
			}
			attrs.push({
github canjs / canjs / view / stache / html_section.js View on Github external
var HTMLSection = function(process){
	this.data = "targetData";
	this.targetData = [];
	// A record of what targetData element we are within.
	this.targetStack = [];
	var self = this;
	this.targetCallback = function(scope, options, sectionNode){
		process.call(this,
			scope,
			options,
			sectionNode,
			can.proxy(self.compiled.hydrate, self.compiled),
			self.inverseCompiled && can.proxy(self.inverseCompiled.hydrate, self.inverseCompiled)  ) ;
	};
};
can.extend(HTMLSection.prototype,{
	inverse: function(){
		this.inverseData = [];
		this.data = "inverseData";
	},
	// Adds a DOM node.
	push: function(data){
		this.add(data);
		this.targetStack.push(data);
	},
	pop: function(){
		return this.targetStack.pop();
	},
	add: function(data){
		if(typeof data === "string"){
			data = decodeHTML(data);
		}
github canjs / can-derive / compute-collection / compute-collection.js View on Github external
_bindsetup: function () {
        var self = this;

        // Mixin the methods used to store/manage the metadata associated
        // with each item in the map/list
        if (this._source instanceof List) {
            can.extend(this, ListCollection);
        } else if (this._source instanceof Map) {
            can.extend(this, MapCollection);
        }

        // Initialize the collection
        this._collection = this._makeStore();

        // Index the current set of items
        this._source.each(function (item, attr) {
            self._addItem(attr);
        });

        // Handle the adding/removing of items
        this._subscribe();
    },
    _bindToKeyValueChange: function (sourceKey) {
github canjs / canjs / view / stache / text_section.js View on Github external
var can = require('can/util/util');
var live = require('can/view/live/live');
var utils = require('./utils');
var liveStache = require('./live_attr');

var TextSectionBuilder = function(){
	this.stack = [new TextSection()];
};

can.extend(TextSectionBuilder.prototype,utils.mixins);

can.extend(TextSectionBuilder.prototype,{
	// Adds a subsection.
	startSection: function(process){
		var subSection = new TextSection();
		this.last().add({process: process, truthy: subSection});
		this.stack.push(subSection);
	},
	endSection: function(){
		this.stack.pop();
	},
	inverse: function(){
		this.stack.pop();
		var falseySection = new TextSection();
		this.last().last().falsey = falseySection;
		this.stack.push(falseySection);
github canjs / canjs / map / lazy / bubble.js View on Github external
var can = require('can/util/util');
require('can/map/bubble');

var bubble = can.bubble;

module.exports = can.extend({}, bubble, {
	childrenOf: function (parentMap, eventName) {
		if(parentMap._nestedReference) {
			parentMap._nestedReference.each(function (child, ref) {
				if (child && child.bind) {
					bubble.toParent(child, parentMap, ref(), eventName);
				}
			});
		} else {
			bubble._each.apply(this, arguments);
		}
	}
});
github canjs / canjs / view / stache / text_section.js View on Github external
}
		};
	}
});

var passTruthyFalsey = function(process, truthy, falsey){
	return function(scope, options){
		return process.call(this, scope, options, truthy, falsey);
	};
};

var TextSection = function(){
	this.values = [];
};

can.extend( TextSection.prototype, {
	add: function(data){
		this.values.push(data);
	},
	last: function(){
		return this.values[this.values.length - 1];
	},
	compile: function(){
		var values = this.values,
			len = values.length;

		for(var i = 0 ; i < len; i++) {
			var value = this.values[i];
			if(typeof value === "object") {
				values[i] = passTruthyFalsey( value.process,
				    value.truthy && value.truthy.compile(),
				    value.falsey && value.falsey.compile());