AmitDhanawat / jquery-multi-open-accordion

Automatically exported from code.google.com/p/jquery-multi-open-accordion
0 stars 0 forks source link

Array.prototype.hasObject #8

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Try iterating an array in your Javascript like var arr = [];
for (var index arr) {
   alert(index)
}

What is the expected output? What do you see instead?
Nothin, as the array is empty.
I see hasObject.

What version of the product are you using? On what operating system?
Chrome, Javascript.

Please provide any additional information below.

Use a different method instead of the Array.prototype.hasObject extension as 
that is messing up the javascript indexes for non key based array.

Original issue reported on code.google.com by alab...@gmail.com on 30 Sep 2011 at 1:24

GoogleCodeExporter commented 9 years ago
Modified:
    // helper array has object function
    // thanks to @Vinko Vrsalovic
    // http://stackoverflow.com/questions/143847/best-way-to-find-an-item-in-a-javascript-array

WAS incompatible to below:

    var hasObject = function (arr, o) {
        var l = arr.length + 1;
        while (l -= 1) {
            if (arr[l - 1] === o) {
                return true;
            }
        }
        return false;
    };

Then:
        // setting array of active tabs
        _setActiveTabs: function (tabs) {
            var self = this;
            var $this = this.element;
            if (typeof tabs != 'undefined') {
                $this.children('div').each(function (index) {
                    var $tab = $(this).prev('h3');
                    if (hasObject(tabs, index)) {
                        self._showTab($tab);
                    } else {
                        self._hideTab($tab);
                    }
                });
            }
        },

See usage of has object. No does not mess up lots of JS.

Original comment by alab...@gmail.com on 30 Sep 2011 at 1:30

GoogleCodeExporter commented 9 years ago
See attached example.
Please fix it by next release so no worry anymore about this!

Original comment by alab...@gmail.com on 30 Sep 2011 at 1:31

Attachments:

GoogleCodeExporter commented 9 years ago
Here's a better fix using a feature jQuery already has:

@@ -181,7 +181,7 @@
            if(typeof tabs != 'undefined') {
                $this.children('div').each(function(index){
                    var $tab = $(this).prev('h3');
-                   if(tabs.hasObject(index)) {
+                   if(jQuery.inArray(index,tabs) != -1) {
                        self._showTab($tab);
                    } else {
                        self._hideTab($tab);
@@ -244,25 +244,5 @@
        }

    });
-   
-   // helper array has object function
-   // thanks to @Vinko Vrsalovic
-   // 
http://stackoverflow.com/questions/143847/best-way-to-find-an-item-in-a-javascri
pt-array
-
-   /*
-   Array.prototype.hasObject = (!Array.indexOf ? function (k) {
-       var l = this.length + 1;
-       while (l -= 1) {
-           if (this[l - 1] === k) {
-               return true;
-           }
-       }
-       return false;
-     }: function (k) {
-       return (this.indexOf(o) !== -1);
-     }
-   );
-   */
-   
 })(jQuery);

Original comment by gameshow...@gmail.com on 8 Jun 2012 at 7:17

Attachments: