StyleWriter

docs

In many of our libraries we inject styles for layouts by default. We don't want to inject the style more than we have to (i.e. more than once) and we want to give users (people using our code, not visitors to sites) the option to override those styles. We do this by injecting the stylesheets into the document head only if they aren't already there. So if you want to use our styles you don't have to do anything, and if you want to override them you just create a style element with the proper id and our library will skip injecting it's own.

Note that this only refers to injecting css instructions into the page, not to injecting links to external css documents.

Because we found ourselves doing this often, we wrote a little class (StyleWriter) to be implemented into the classes that need this functionality.

//implemented into a class (intended use)
var MyClass = new Class({
    Implements: [StyleWriter],
    //...
});
new MyClass().createStyle("a.example { color: red }", "redLinks");
//standalone
new StyleWriter().createStyle("a.example { color: red }", "redLinks"); 
// just exits as 'redLinks' already exists

cnet-libraries/07-ui/04-stylewriter.txt · Last modified: 2008/11/17 23:25