Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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);
}
}
});
originalSetup.apply(this, arguments);
}
});
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);
});
}
var promise = connection[method](hash);
addToPageData(hash, promise);
viewModel,
frag,
// an array of teardown stuff that should happen when the element is removed
teardownFunctions = [],
callTeardownFunctions = function() {
for (var i = 0, len = teardownFunctions.length; i < len; i++) {
teardownFunctions[i]();
}
},
$el = can.$(el),
setupBindings = !can.data($el, "preventDataBindings");
// ## Scope
// Add viewModel prototype properties marked with an "@" to the `initialViewModelData` object
can.each(this.constructor.attributeScopeMappings, function(val, prop) {
initialViewModelData[prop] = el.getAttribute(can.hyphenate(val));
});
if (setupBindings) {
teardownFunctions.push(bindings.behaviors.viewModel(el, componentTagData, function(initialViewModelData) {
// Make %root available on the viewModel.
initialViewModelData["%root"] = componentTagData.scope.attr("%root");
// Create the component's viewModel.
var protoViewModel = component.viewModel || component.scope;
if (component.constructor.Map) {
// If `Map` property is set on the constructor use it to wrap the `initialViewModelData`
viewModel = new component.constructor.Map(initialViewModelData);
} else if (protoViewModel instanceof can.Map) {
replace: function (oldElements, newFrag) {
// The following helps make sure that a selected <option> remains
// the same by removing `selected` from the currently selected option
// and adding selected to an option that has the same value.
var selectedValue,
parentNode = oldElements[0].parentNode;
if(parentNode.nodeName.toUpperCase() === "SELECT" && parentNode.selectedIndex >= 0) {
selectedValue = parentNode.value;
}
elements.after(oldElements, newFrag);
if(can.remove(can.$(oldElements)).length < oldElements.length && !selectsCommentNodes) {
can.each(oldElements, function(el) {
if(el.nodeType === 8) {
el.parentNode.removeChild(el);
}
});
}
if(selectedValue !== undefined) {
parentNode.value = selectedValue;
}
}
};</option>
// 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] = [];
}
errors[attr].push(res);
}
});
}, validations = this.constructor.validations || {},
isTest = attrs && attrs.length === 1 && arguments.length === 2;
// go through each attribute or validation and
// add any errors
can.each(attrs || validations, function (funcs, attr) {
// if we are iterating through an array, use funcs
// as the attr name
if (typeof attr === 'number') {
attr = funcs;
funcs = validations[attr];
}
// add errors to the
addErrors(attr, funcs || []);
});
// return errors as long as we have one
return can.isEmptyObject(errors) ? null : isTest ? errors[attrs[0]] : errors;
}
});
var validate = function (attrNames, options, proc) {
// normalize argumetns
if (!proc) {
proc = options;
options = {};
}
options = options || {};
attrNames = typeof attrNames === 'string' ? [attrNames] : can.makeArray(attrNames);
// run testIf if it exists
if (options.testIf && !options.testIf.call(this)) {
return;
}
var self = this;
can.each(attrNames, function (attrName) {
// Add a test function for each attribute
if (!self.validations[attrName]) {
self.validations[attrName] = [];
}
self.validations[attrName].push(function (newVal) {
// if options has a message return that, otherwise, return the error
var res = proc.call(this, newVal, attrName);
return res === undefined ? undefined : options.message || res;
});
});
};
var old = can.Map.prototype.__set;
cacheItems: function (items) {
var data = this._cached,
id = this.id;
can.each(items, function (item) {
var idVal = item[id],
obj = data[idVal];
if (obj) {
can.extend(obj, item);
} else {
data[idVal] = item;
}
});
window.localStorage.setItem(this.cachedKey(), JSON.stringify(data));
},
findAllCached: function (params) {
this._source.bind('add', function (ev, newItems, offset) {
can.each(newItems, function (item, i) {
var sourceIndex = offset + i;
self._addItem(sourceIndex);
});
});
this._source.bind('remove', function (ev, removedItems, offset) {
can.each(removedItems, function (item, i) {
var sourceIndex = offset + i;
self._removeItem(sourceIndex);
});
});
},
each: function (callback) {
var self = this;
can.each(this.references, function (ref) {
var path = ref();
callback(self.get(path), ref, path);
});
}