zjsquery: Add data support.

Before this we just noop'ed it, since at one time
we were trying to deprecate this is in favor
of attr calls.
This commit is contained in:
Steve Howell 2020-01-03 16:10:59 +00:00 committed by Tim Abbott
parent 9ba1829243
commit 493afcb9f0
2 changed files with 16 additions and 1 deletions

View File

@ -50,6 +50,11 @@ run_test('basics', () => {
widget.attr('data-employee-id', 42);
assert.equal(widget.attr('data-employee-id'), 42);
assert.equal(widget.data('employee-id'), 42);
widget.data('department-id', 77);
assert.equal(widget.attr('data-department-id'), 77);
assert.equal(widget.data('department-id'), 77);
widget.html('<b>hello</b>');
assert.equal(widget.html(), '<b>hello</b>');

View File

@ -177,7 +177,17 @@ exports.make_new_elem = function (selector, opts) {
}
return elem.parent().closest(selector);
},
data: noop,
data: function (name, val) {
if (val === undefined) {
const data_val = attrs.get('data-' + name);
if (data_val === undefined) {
return;
}
return JSON.parse(data_val);
}
attrs.set('data-' + name, val);
return self;
},
delay: function () {
return self;
},