mirror of
https://github.com/xibyte/jsketcher
synced 2025-12-12 03:13:24 +01:00
26 lines
No EOL
643 B
JavaScript
26 lines
No EOL
643 B
JavaScript
import {sprintf} from 'sprintf'
|
|
|
|
export function Bind(dom, data, policy) {
|
|
if (!policy) policy = DEFAULT_POLICY;
|
|
const props = Object.getOwnPropertyNames(data);
|
|
for (let prop of props) {
|
|
const node = $(dom).find('[data-bind="'+prop+'"]');
|
|
if (node.length == 0) continue;
|
|
let value = data[prop];
|
|
if (!policy.hideEmptyValue || value || value === 0) {
|
|
var format = node.attr('bind-format');
|
|
if (format) {
|
|
value = sprintf(format, value);
|
|
}
|
|
node.text(value);
|
|
node.show();
|
|
} else {
|
|
node.text('');
|
|
node.hide();
|
|
}
|
|
}
|
|
}
|
|
|
|
const DEFAULT_POLICY = {
|
|
hideEmptyValue: true
|
|
}; |