Configure a component and reconfigure it later
The second argument of COMPONENT() holds the default options. The config attribute of the element overrides them, and the declaration reads the merged values from config. Implement self.configure(key, value) to react when an option is set or changed. At runtime self.reconfigure('key:value;key2:value2') (or CONFIG(selector, config) for many components) changes options and calls configure for each changed key.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>jComponent</title>
<link rel="stylesheet" href="//cdn.componentator.com/spa.min@20.css" />
<script src="//cdn.componentator.com/spa.min@20.js"></script>
</head>
<body>
<ui-component name="badge" config="label:Sale"></ui-component>
<ui-component name="badge" id="second"></ui-component>
<script>
COMPONENT('badge', 'color:blue;label:New', function(self, config) {
self.make = function() {
self.html('<b class="label"></b>');
self.draw();
};
self.draw = function() {
self.find('.label').text(config.label).css('color', config.color);
};
self.configure = function(key, value) {
self.find('.label').length && self.draw();
};
});
</script>
</body>
</html> Try it in your browser
Edit the example and run it. It runs in an isolated frame in your browser; nothing is sent to our server.
Preview