jsketcher/web/app/3d/ui/bind.js
2016-10-14 01:19:06 -07:00

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
};