Changeset 12899

Show
Ignore:
Timestamp:
03/05/08 00:00:44 (9 months ago)
Author:
alex
Message:

adding sugar to dojo.attr(), and therefore to it's mapping into dojo.NodeList?. Expanding tests and docs. Supports a property-bag style for attrs the same way we do with dojo.style(). Refs #4205. !strict

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • dojo/trunk/_base/html.js

    r12893 r12899  
    10231023        } 
    10241024 
    1025         dojo.attr = function(/*DomNode|String*/node, /*String*/name, /*String?*/value){ 
     1025        dojo.attr = function(/*DomNode|String*/node, /*String|Object*/name, /*String?*/value){ 
    10261026                //      summary: 
    10271027                //              Gets or sets an attribute on an HTML element. 
    10281028                //      description: 
    1029                 //              If 2 arguments are passed, acts as a getter. 
    1030                 //              If a third argument is passed, acts as a setter. 
     1029                //              Handles normalized getting and setting of attributes on DOM 
     1030                //              Nodes. If 2 arguments are passed, and a the second argumnt is a 
     1031                //              string, acts as a getter. 
     1032                //       
     1033                //              If a third argument is passed, or if the second argumnt is a 
     1034                //              map of attributes, acts as a setter. 
    10311035                //      node: 
    10321036                //              id or reference to the element to get or set the attribute on 
    10331037                //      name: 
    1034                 //              the name of the attribute to get or set 
     1038                //              the name of the attribute to get or set. 
    10351039                //      value: 
    1036                 //              the value for the attribute (optional) 
     1040                //              Optional. The value to set for the attribute 
    10371041                //      returns: 
    10381042                //              when used as a getter, the value of the requested attribute 
    10391043                //              or null if that attribute does not have a specified or 
    10401044                //              default value; 
     1045                // 
    10411046                //              when user as a setter, undefined 
     1047                //      example: 
     1048                //      |       // get the current value of the "foo" attribute on a node 
     1049                //      |       dojo.attr(dojo.byId("nodeId"), "foo"); 
     1050                //      |        
     1051                //      |       // we can just pass the id: 
     1052                //      |       dojo.attr("nodeId", "foo"); 
     1053                //      | 
     1054                //      |       // use attr() to set the tab index 
     1055                //      |       dojo.attr("nodeId", "tabindex", 3); 
     1056                //      | 
     1057                //      |       // set multiple values at once, including event handlers: 
     1058                //      |       dojo.attr("formId", { 
     1059                //      |               "foo": "bar", 
     1060                //      |               "tabindex": -1, 
     1061                //      |               "method": "POST", 
     1062                //      |               "onsubmit": function(e){ 
     1063                //      |                       dojo.stopEvent(e); 
     1064                //      |                       // submit the form with Ajax 
     1065                //      |                       dojo.xhrPost({ form: "formId" }); 
     1066                //      |               } 
     1067                //      |       }); 
     1068 
     1069                var args = arguments.length; 
     1070                if(args == 2 && !dojo.isString(name)){ 
     1071                        for(var x in name){ dojo.attr(node, x, name[x]); } 
     1072                        return; 
     1073                } 
    10421074                node = dojo.byId(node); 
    10431075                name = _fixAttrName(name); 
    1044                 if(arguments.length == 3){ 
    1045                         if(typeof value == "function" || typeof value == "boolean"){ // e.g. onsubmit, disabled 
     1076                if(args == 3){ 
     1077                        if(dojo.isFunction(value)){ 
     1078                                delete node[name]; // preserve clobbering behavior 
     1079                                // ensure that event objects are normalized, etc. 
     1080                                dojo.connect(node, name, value); 
     1081                        }else if(typeof value == "boolean"){ // e.g. onsubmit, disabled 
     1082                                // if a function, we should normalize the event object here!!! 
    10461083                                node[name] = value; 
    10471084                        }else{ 
    10481085                                node.setAttribute(name, value); 
    10491086                        } 
    1050                         return undefined; 
     1087                        return; 
    10511088                }else{ 
    10521089                        // should we access this attribute via a property or