]> source.dussan.org Git - jquery-ui.git/commitdiff
Core: Fixed #3257: Added option method to widget factory.
authorScott González <scott.gonzalez@gmail.com>
Wed, 27 Aug 2008 02:44:12 +0000 (02:44 +0000)
committerScott González <scott.gonzalez@gmail.com>
Wed, 27 Aug 2008 02:44:12 +0000 (02:44 +0000)
ui/ui.core.js

index 2023e29114327638873f3bc346a1b2f77ddec8de..3d78c973a69a3ab56bd69a4510cd5326936ae631 100644 (file)
@@ -52,9 +52,16 @@ $.keyCode = {
 // $.widget is a factory to create jQuery plugins
 // taking some boilerplate code out of the plugin code
 // created by Scott González and Jörn Zaefferer
-function getter(namespace, plugin, method) {
-       var methods = $[namespace][plugin].getter || [];
-       methods = (typeof methods == "string" ? methods.split(/,?\s+/) : methods);
+function getter(namespace, plugin, method, args) {
+       function getMethods(type) {
+               var methods = $[namespace][plugin][type] || [];
+               return (typeof methods == 'string' ? methods.split(/,?\s+/) : methods);
+       }
+       
+       var methods = getMethods('getter');
+       if (args.length == 1 && typeof args[0] == 'string') {
+               methods = methods.concat(getMethods('getterSetter'));
+       }
        return ($.inArray(method, methods) != -1);
 }
 
@@ -73,7 +80,7 @@ $.widget = function(name, prototype) {
                }
                
                // handle getter methods
-               if (isMethodCall && getter(namespace, name, options)) {
+               if (isMethodCall && getter(namespace, name, options, args)) {
                        var instance = $.data(this[0], name);
                        return (instance ? instance[options].apply(instance, args)
                                : undefined);
@@ -120,6 +127,10 @@ $.widget = function(name, prototype) {
        
        // add widget prototype
        $[namespace][name].prototype = $.extend({}, $.widget.prototype, prototype);
+       
+       // TODO: merge getter and getterSetter properties from widget prototype
+       // and plugin prototype
+       $[namespace][name].getterSetter = 'option';
 };
 
 $.widget.prototype = {
@@ -128,6 +139,22 @@ $.widget.prototype = {
                this.element.removeData(this.widgetName);
        },
        
+       option: function(key, value) {
+               var options = key,
+                       self = this;
+               
+               if (typeof key == "string") {
+                       if (value === undefined) {
+                               return this._getData(key);
+                       }
+                       options = {};
+                       options[key] = value;
+               }
+               
+               $.each(options, function(key, value) {
+                       self._setData(key, value);
+               });
+       },
        _getData: function(key) {
                return this.options[key];
        },