--- layout : 'default' css : 'module' title : 'UI Modules' description : 'Modules are interface elements whose behavior is an essential part of their definition' type : 'Semantic' --- <%- @partial('header') %>

Design Pattern

All official javascript modules in Semantic are designed using a singular design pattern. This pattern allows several useful features common to all javascript components

View Commented Example
Run-time Performance Analysis

Semantic modules all provide the ability to log performance traces to the console, allowing you to see which aspects of the module are more or less performant and to track total init time onDomReady

Human Readable Traces

Unlike other component libraries which hides explanations of behavior in inline comments which can only be read by combing the source, semantic modules provide run-time debug output to the javascript console telling you what each component is doing as it is doing it.

Settings can be overwritten after initialization

Semantic provides methods to set default settings, set settings at initialization, and set settings after initialization, allowing complete flexibility over component behaviors.

All modules include an initialize and destroy method

All events and metadata are namespaced and can be removed after initialization, modules automatically handle destroy/init events to allow users to lazy-initialize a plugin multiple times with no issues.

Instance available in metadata

Modules store their instance in metadata meaning that, in a pinch, you can directly modify the instance of a UI element by modifying its properties.

Behaviors

Behaviors are an elements API. These can be invoke functionality or return aspects of the current state for an element

Triggering a Behavior

Behaviors are triggered in Semantic using a familiar syntax. API behaviors can be called using spaced words, camelcase or dot notation. The preferred method however is spaced words. Method lookup is done internally.

// generically $('.ui.element') .module('method', valueOne, valueTwo) ; // Sets a popup to top left position with an offset of negative five $('.ui.popup') .popup('set position', 'top left', -5) ;

Common Behaviors

All modules have a set of core behaviors which allow you to configure the component

Name Usage
initialize Initializes an element, adding page event handlers and init data
destroy Removes all changes to the page made by initialization
refresh Refreshes cached values for a component
setting(setting, value) Allows you to modify or read a component setting
invoke(query, passedArguments, instance) Internal method used for translating sentence case method into an internal method
log(comment, values) Displays a log if a user has logging enabled
verbose(comment, values) Displays a log if a user has verbose logging enabled
error(name) Displays a name error message from the component's settings
performance log(comment, value) Adds a performance trace for an element
performance display Displays current element performance trace

Settings

Unlike many javascript components, anything arbitrary in Semantic is a setting. This means no need to dig inside the internals of the component to alter an expected css selector or class name, simply alter the settings object

Common Settings

That means class names, selectors, error output, dom metadata etc can all be controlled by altering the settings object.

The following is a list of common settings usually found in each javascript module.
Name Usage
name Name used in debug logs to differentiate this widget from other debug statements.
debug Provides standard debug output to console.
performance Provides performance logging to console of internal method calls.
verbose Provides extra debug output to console
namespace Namespace used for DOM event and metadata namespacing. Allows module's destroy method to not affect other modules.
metadata An object containing any metadata attributes used.
selectors An object containing all selectors used in the module, these are usually children of the module.
classNames An object containing all classnames used in the module.
errors A javascript array of error statements used in the plugin. These may sometimes appear to the user, but most often appear in debug statements.

Changing Settings

The following examples use popup as an example for how to modify settings

Setting module defaults
Default settings for the module can be overridden by modifying $.fn.module.settings.
$.fn.popup.settings.moduleName = 'Godzilla';
At initialization
A settings object can be passed in when initializing the plugin
$('.foo') .popup({ moduleName : 'Godzilla', verbose : true }) ;
After initialization
Settings can be changed after a module is initialized by calling the 'settings' method on the module with either a settings object or a name, value pair.
$('.foo') // lets initialize that! .popup() // oh wait forgot something .popup('setting', 'moduleName', 'Godzilla') // and some more things .popup('setting', { debug : true, verbose : true }) ;

Reading Settings

Settings can also be read programmatically:

// outputs godzilla $('.foo').popup('setting', 'moduleName');