I have the following code:
let value = $(this).data('value');
value = System.decrypt(value, key);
$(this).data('value', value);
I’d like it to support chaining:
$(this)
.data('value', (value) => System.decrypt(value, key))
.css(…)
.prop(…);
But as far as I see, jQuery doesn’t have a lambda version of .data()
. Is there a simple way to achieve this, or I should write a plugin?
2
Well, I wrote the following simple helpers:
// Updates a key. Chaining compatible.
jQuery.fn.updateData = function (key, updateCallback)
{
this.data(key, updateCallback(this.data(key)));
return this;
};
// Updates an attribute. Chaining compatible.
jQuery.fn.updateAttr = function (attributeName, updateCallback)
{
this.attr(attributeName, updateCallback(this.attr(attributeName)));
return this;
};
// Updates a property. Chaining compatible.
jQuery.fn.updateProp = function (propName, updateCallback)
{
this.prop(propName, updateCallback(this.prop(propName)));
return this;
};
// Updates a value. Chaining compatible.
jQuery.fn.updateVal = function (updateCallback)
{
this.val(updateCallback(this.val()));
return this;
};