Plugin Class
Item Index
Methods
- Plugin.addHostAttr static
Methods
Plugin.addHostAttr
-
name -
host -
plugin -
[setter] -
[force]
Register a Plugin with an activation attribute on a host class. Setting this attribute at construction or at run time will cause the Plugin to be plugged into the instance.
By default, trigger attributes will support values true or a configuration
object to plug() the plugin and false to unplug() it.
To support enhancing host instance behavior when the plugin is
use()d after the host instance is instantiated, you can also pass the
instance as the second parameter.
To allow custom values to be passed to the trigger attribute, pass a
preprocessor function as the fourth parameter. The value assigned to the
attribute will be translated by this function prior to getting passed to
plug() as the configuration. Return false from this function to cause
the plugin to be unplugged.
The host class must have a static ATTRS collection.
Parameters:
-
nameStringThe attribute name to trigger plug and unplug
-
hostFunction | ObjectThe class or instance to receive the triggering attribute
-
pluginFunctionThe plugin class
-
[setter]Function optionalAttribute value preprocessor
-
[force]Boolean optionalRedefine an existing host attribue?
Example:
Add "draggable" triggering attribute to Y.DataTable.Base:
Y.Plugin.addHostAttr('sortable', Y.DataTable.Base, Y.Plugin.DataTableSort);
var table = new Y.DataTable({ sortable: true }); // plugs DTSort
table.set('sortable', false); // unplugs DTSort
Add support for custom values passed to the triggering attribute
// Add a triggering attribute "filters" that accepts true|false or
// a configuration object (out-of-the-box support), as well as a single
// string or string array to pass as the plugin's "category" configuration
Y.Plugin.addHostAttr('filters', Y.Console, Y.Plugin.ConsoleFilters,
function (config) {
if (Y.Lang.isString(config) || Y.Lang.isArray(config)) {
config = {
defaultVisibility: false,
category: Y.Array.hash(Y.Array(config))
};
}
return config;
}
});
var con = new Y.Console({ filters: ['warn', 'error'] });
undefined: Plugin