Mixing in defaults

Colin Clark colin.clark at utoronto.ca
Mon May 12 14:41:19 UTC 2008


Hey guys,

We've successfully established a pattern with Fluid components where  
they provide a set of defaults that can be selectively overridden by  
the developer using the component. For example, CSS class names can be  
customized so that you can have multiple versions of a component on  
the same page, each with their own separate styles. DOM selectors can  
similarly be customized to allow for highly flexible markup.

I've noticed some inconsistency in the way we implement this. In  
fluid.utils, we've got a function called initCssClassNames() that,  
despite the name, is a general way of merging user-specified values  
with defaults.  Here's how it works:

     fluid.utils.initCssClassNames = function (defaultNames,  
classNames) {
         if (!classNames) {
             return defaultNames;
         }
         var cssClassNames = {};
         for (var className in defaultNames) {
             if (defaultNames.hasOwnProperty (className)) {
                 cssClassNames[className] = classNames[className] ||  
defaultNames[className];
             }
         }

         return cssClassNames;
     };

I've used a different approach that is more succinct:

     jQuery.extend({}, defaultNames, userNames);

Do you have any preference for which approach we use? I tend to prefer  
any solution that has less lines of code, but there's clearly an  
effort in initCssClassNames() to ensure a 1:1 mapping of the defaults  
and the user-specified overrides.

Thoughts?

Colin

---
Colin Clark
Technical Lead, Fluid Project
Adaptive Technology Resource Centre, University of Toronto
http://fluidproject.org




More information about the fluid-work mailing list