thanhhovn / sharpkit

Automatically exported from code.google.com/p/sharpkit
0 stars 0 forks source link

jQuery class is not enumerable #258

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
The class implements IEnumerable, but the implementation throws 
NotImplementedException.

Original issue reported on code.google.com by yvan.rod...@gmail.com on 5 Dec 2012 at 6:19

GoogleCodeExporter commented 8 years ago
Are you running it on the client side? because it should work, can you provide 
the generated js code when enumerating a jquery object?

Thanks
Dan-el

Original comment by DanelK...@gmail.com on 5 Dec 2012 at 6:55

GoogleCodeExporter commented 8 years ago
Issue 220 has been merged into this issue.

Original comment by yvan.rod...@gmail.com on 13 Jan 2013 at 1:51

GoogleCodeExporter commented 8 years ago
Workaround: add this to javascript code:

jQuery.fn.GetEnumerator = function () {
    return new ArrayEnumerator.ctor(this);
}

Original comment by yvan.rod...@gmail.com on 13 Jan 2013 at 1:55

GoogleCodeExporter commented 8 years ago
Dan-el, here is an example:

       private void ToArrayBug()
        {
            var sample = new jQuery("<ul><li>1</li><li>2</li><li>3</li><li>4</li>");

            var sampleEnumerable = from s in sample.children() select s;
            var sampleArray1 = sampleEnumerable.ToArray();

            var sampleList = new List<HtmlElement>();
            foreach(var s in sample.children())
                sampleList.Add(s);
            var sampleArray2 = sampleList.ToArray();
        }

compiles to:

        ToArrayBug: function ()
        {
            var sample = $("<ul><li>1</li><li>2</li><li>3</li><li>4</li>");
            var sampleEnumerable = System.Linq.Enumerable.Select$2$$IEnumerable$1$$Func$2(HTMLElement, HTMLElement, sample.children(), $CreateAnonymousDelegate(this, function (s)
            {
                return s;
            }));
            var sampleArray1 = System.Linq.Enumerable.ToArray$1(HTMLElement, sampleEnumerable);
            var sampleList = new System.Collections.Generic.List$1.ctor(HTMLElement);
            for (var $i20 = 0, $t20 = sample.children(), $l20 = $t20.length, s = $t20[$i20]; $i20 < $l20; $i20++, s = $t20[$i20])
                sampleList.Add(s);
            var sampleArray2 = sampleList.ToArray();
        }

which throws "Uncaught TypeError: Object [object Object] has no method 
'GetEnumerator'" in 
System.Linq.Enumerable.WhereSelectEnumerableIterator.MoveNext(). It's correct.

Original comment by yvan.rod...@gmail.com on 13 Jan 2013 at 2:00

GoogleCodeExporter commented 8 years ago
Yes, jQuery implements IEnumerable in order to enable foreach, although it also 
implements IJsEnumerable, in order to identify the class as native enumerable, 
which means that it's possible to iterate it like an array. You're correct that 
this might be confusing, although unless you're using clr mode, you're not 
supposed to use LINQ. If you are, then it's possible to inject your suggested 
code in order to properly support it.

Original comment by DanelK...@gmail.com on 14 Jan 2013 at 8:24