API Docs for:
Show:

Plugin Class

Extension For

Item Index

Methods

Methods

Plugin.addHostAttr

(
  • name
  • host
  • plugin
  • [setter]
  • [force]
)
static

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:

  • name String

    The attribute name to trigger plug and unplug

  • host Function | Object

    The class or instance to receive the triggering attribute

  • plugin Function

    The plugin class

  • [setter] Function optional

    Attribute value preprocessor

  • [force] Boolean optional

    Redefine 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'] });