How to use the can/util/util.compute 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 / component / component.js View on Github external
var hasObjectLookup, readyCompute;

			paramReplacer.lastIndex = 0;

			hasObjectLookup = paramReplacer.test(methodName);

			// If we don't have options (a `control` instance), we'll run this
			// later.
			if (!controlInstance && hasObjectLookup) {
				return;
			} else if (!hasObjectLookup) {
				return can.Control._action.apply(this, arguments);
			} else {
				// We have `hasObjectLookup` and `controlInstance`.

				readyCompute = can.compute(function() {
					var delegate;

					// Set the delegate target and get the name of the event we're listening to.
					var name = methodName.replace(paramReplacer, function(matched, key) {
						var value;

						// If we are listening directly on the `viewModel` set it as a delegate target.
						if (key === "scope" || key === "viewModel") {
							delegate = options.viewModel;
							return "";
						}

						// Remove `viewModel.` from the start of the key and read the value from the `viewModel`.
						key = key.replace(/^(scope|^viewModel)\./, "");
						value = can.compute.read(options.viewModel, can.compute.read.reads(key), {
							// if we find a compute, we should bind on that and not read it
github canjs / can-derive / compute-collection / compute-collection.js View on Github external
computes.activated = can.compute(false);

        // A compute that holds a reference to the source map/lists value
        // NOTE: This value is updated by the map/lists _subscribe function
        computes.sourceValue = can.compute(this._source.attr(sourceKey));
        // The key that's used to read the source value of this item from
        // the source map/list
        computes.sourceKey = can.compute(sourceKey);

        // A compute bound to the item's key
        computes.key = can.compute(makeComputeFn('keyFn'), this);

        // A compute bound to the item's value
        // TODO: Find out if you can update a compute's function. This way
        // no computes are created unless you provide a valueFn.
        computes.value = can.compute(makeComputeFn('valueFn'), this);

        return computes;
    },
    _addItem: function (sourceKey) {
github canjs / can-derive / compute-collection / compute-collection.js View on Github external
// want unnecessary executions of these computes if we can
                // help it.

                return fn(item, i);
            };
        };

        // A flag that enables/disables the key/value compute
        computes.activated = can.compute(false);

        // A compute that holds a reference to the source map/lists value
        // NOTE: This value is updated by the map/lists _subscribe function
        computes.sourceValue = can.compute(this._source.attr(sourceKey));
        // The key that's used to read the source value of this item from
        // the source map/list
        computes.sourceKey = can.compute(sourceKey);

        // A compute bound to the item's key
        computes.key = can.compute(makeComputeFn('keyFn'), this);

        // A compute bound to the item's value
        // TODO: Find out if you can update a compute's function. This way
        // no computes are created unless you provide a valueFn.
        computes.value = can.compute(makeComputeFn('valueFn'), this);

        return computes;
    },
    _addItem: function (sourceKey) {
github bitovi-components / bit-c3 / bit-c3 / node_modules / can-connect / src / can / tag / tag.js View on Github external
var addedToPageData = false;
		var addToPageData = can.__notObserve(function(set, promise){
			if(!addedToPageData) {
				var root = tagData.scope.attr("%root") || tagData.scope.attr("@root");
				if( root && root.pageData ) {
					if(method === "get"){
						set = connection.id(set);
					}
					root.pageData(connection.name, set, promise);
				}
			}
			addedToPageData = true;
		});

		var request = can.compute(function(){
			var hash = {};
			if(typeof attrInfo.hash === "object") {
				// old expression data
				can.each(attrInfo.hash, function(val, key) {
					if (val && val.hasOwnProperty("get")) {
						hash[key] = tagData.scope.read(val.get, {}).value;
					} else {
						hash[key] = val;
					}
				});
			} else {
				// new expression data
				var getHash = attrInfo.hash(tagData.scope, tagData.options, {});
				can.each(getHash(), function(val, key) {
					hash[key] = convertToValue(val);
				});
github canjs / can-derive / compute-collection / compute-collection.js View on Github external
return fn(item, i);
            };
        };

        // A flag that enables/disables the key/value compute
        computes.activated = can.compute(false);

        // A compute that holds a reference to the source map/lists value
        // NOTE: This value is updated by the map/lists _subscribe function
        computes.sourceValue = can.compute(this._source.attr(sourceKey));
        // The key that's used to read the source value of this item from
        // the source map/list
        computes.sourceKey = can.compute(sourceKey);

        // A compute bound to the item's key
        computes.key = can.compute(makeComputeFn('keyFn'), this);

        // A compute bound to the item's value
        // TODO: Find out if you can update a compute's function. This way
        // no computes are created unless you provide a valueFn.
        computes.value = can.compute(makeComputeFn('valueFn'), this);

        return computes;
    },
    _addItem: function (sourceKey) {
github canjs / canjs / view / href / href.js View on Github external
can.view.attr("can-href", function(el, attrData){

	// foo='bar' zed=5 abc=myValue
	// Note: 'tmp ' is added because expressionData "Breaks up the name and arguments of a stache expression.", but we don't use name:
	var attrInfo = expression.parse('tmp(' + removeCurly(el.getAttribute("can-href"))+")", {baseMethodType: "Call"});
	var getHash = attrInfo.hash(attrData.scope, null);
	// -> {hash: {foo: 'bar', zed: 5, abc: {get: 'myValue'}}}
	var routeHref = can.compute(function(){
		return can.route.url(getHash());
	});


	el.setAttribute("href", routeHref());

	var handler = function(ev, newVal){
		el.setAttribute("href", newVal);
	};

	routeHref.bind("change", handler );

	can.bind.call(el,"removed", function(){
		routeHref.unbind("change", handler );
	});
});
github canjs / canjs / map / list / list.js View on Github external
var generator = function (element, index) {
			// The can.compute for the mapping
			var compute = can.compute(function () {
				return callback(element, index, self);
			});
			compute.bind('change', function (ev, val) {
				// On change, replace the current value with the new one
				mapped.splice(index, 1, val);
			});
			mapped.splice(index, 0, compute());
		};
		this.forEach(generator);
github canjs / can-derive / compute-collection / compute-collection.js View on Github external
// Get the value from the source list/map
                item = computes.sourceValue();

                // Pass functions so that bindings aren't created unless
                // they're actually needed. We'll be updating .attr
                // and .sortKey a lot (in the case of lists), so we don't
                // want unnecessary executions of these computes if we can
                // help it.

                return fn(item, i);
            };
        };

        // A flag that enables/disables the key/value compute
        computes.activated = can.compute(false);

        // A compute that holds a reference to the source map/lists value
        // NOTE: This value is updated by the map/lists _subscribe function
        computes.sourceValue = can.compute(this._source.attr(sourceKey));
        // The key that's used to read the source value of this item from
        // the source map/list
        computes.sourceKey = can.compute(sourceKey);

        // A compute bound to the item's key
        computes.key = can.compute(makeComputeFn('keyFn'), this);

        // A compute bound to the item's value
        // TODO: Find out if you can update a compute's function. This way
        // no computes are created unless you provide a valueFn.
        computes.value = can.compute(makeComputeFn('valueFn'), this);
github canjs / canjs / view / stache / text_section.js View on Github external
return function(scope, options){

			var compute = can.compute(function(){
				return renderer(scope, options);
			}, null, false);

			compute.computeInstance.bind("change", can.k);
			var value = compute();
			if( compute.computeInstance.hasDependencies ) {
				if(state.textContentOnly) {
					live.text(this, compute);
				}
				else if(state.attr) {
					live.simpleAttribute(this, state.attr, compute);
				}
				else {
					liveStache.attributes(this, compute, scope, options);
				}
				compute.computeInstance.unbind("change", can.k);
github canjs / can-connect / src / tag.js View on Github external
var addedToPageData = false;
		var addToPageData = can.__notObserve(function(set, promise){
			if(!addedToPageData) {
				var root = tagData.scope.attr("@root");
				if( root && root.pageData ) {
					if(method === "findOne"){
						set = connection.id(set);
					}
					root.pageData(connection.name, set, promise);
				} 
			}
			addedToPageData = true;
		});
	
		var request = can.compute(function(){
			
			var hash = {};
			can.each(attrInfo.hash, function(val, key) {
				if (val && val.hasOwnProperty("get")) {
					hash[key] = tagData.scope.read(val.get, {}).value;
				} else {
					hash[key] = val;
				}
			});
			var promise = connection[method](hash);
			addToPageData(hash, promise);
			return promise;
		});
		
		can.data(can.$(el), "viewModel", request);