-
Notifications
You must be signed in to change notification settings - Fork 0
Base Model
The d2b base model is used in developing most of the components in d2b. It assumes that the JavaScript Module pattern will be used with some interface my and some configuration store config.
# d2b.model.base([my[, config]])
Constructs a new base model. The initial configuration for my and config may be specified. They will both default to {} if unspecified.
# base.base()
Returns the base interface my.
# base.values()
Returns the configuration config.
# base.addProp(key[, value[, fn[, callback]]])
Adds a new property to the my interface identified by the specified key. This new property will act as a getter setter method, when the setter is used the corresponding config attribute will be set to the supplied value. If fn is specified and is non-null it overrides the default getter/setter function. If fn is null and a callback is specified, the callback will be executed after the new property is used as a "setter". Here is an example of using the addProp feature.
base.addProp('width', 500);This is the equivalent of doing the following.
config.width = 500;
my.width = function (val) {
if (!arguments.length) return config.width;
config.width = val;
return my;
};# base.addPropGet(key[, value[, fn])
Adds a new property to the my interface identified by the specified key. This new property will act as a getter only method, If fn is specified and is non-null it overrides the default getter/setter function.
base.addProp('width', 500);This is the equivalent of doing the following.
config.width = 500;
my.width = function () {
return config.width;
};# base.addPropFunctor(key[, value[, fn[, callback]]])
Adds a new functor property to the my interface identified by the specified key. This is helpful if an attribute accessor may be required. If fn is specified and is non-null it overrides the default getter/setter method. If fn is null and a callback is specified, the callback will be executed after the new property is used as a "setter". Here is an example of using the addPropFunctor.
base.addPropFunctor('radius', d => d.r);This is the equivalent of doing the following.
config.radius = (d => d.r);
my.radius = function (val) {
if (!arguments.length) return config.radius;
config.radius = (typeof val === 'function')? val : () => val;
return my;
};# base.removeProp(key)
Removes the property identified by the specified key from the interface my and the configuration config.
# base.addDispatcher(events[, listenerKey[, configKey]])
Adds a new dispatcher with a set of events identified from the supplied events array. This is identified on the config by the supplied configKey, this defaults to "dispatch". The listener setter on the my interface is identified by the listenerKey, this defaults to "on".
base.addDispatcher(["zoomIn", "zoomOut"]);This is the equivalent of doing the following.
config.dispatch = d3.dispatch("zoomIn", "zoomOut");
my.on = function (key, fn) {
if(arguments.length === 0) return config.dispatch;
if(arguments.length === 1) return config.dispatch.on(key);
config.dispatch.on(key, fn);
return my;
};Because the d2b components are meant to be repeatable and reusable, this is rarely used. Instead, events are generally dispatched on d3 selections.
# base.addMethod(key, fn)
Adds a new method fn on the interface my identified by the supplied key.
base.addMethod('update', () => console.log('update it!') );This is the equivalent of doing the following.
my.update = () => console.log('update it!');