Generic jQuery 'each' function

114 views
Skip to first unread message

adriana...@gmail.com

unread,
Oct 12, 2012, 3:40:09 PM10/12/12
to shar...@googlegroups.com
Having a generic collection - I want to be able to iterate it in a strongly-typed way so I have both intellisence and it is able to compile.
For example:

J(list).each<MyType>((index, myType) => // at this point - myType is a strongly-typed object of MyType

Currently each supports only JElement types as a non-generic function so I created my own extension method:

namespace SharpKit.jQuery
{
    public static class Extensions
    {
        public static jQuery each<T>(this jQuery j, JsAction<JsNumber, T> function)
        {
            return j.each(function);
        }

It works but the result js is:
SharpKit.jQuery.Extensions.each$1(..., myCollection...)

instead of:
$(myCollection).each(...

How can I extend jQuery so the compiled js result looks native?


Thanks,
Adrian


Dan-el Khen

unread,
Oct 12, 2012, 3:44:01 PM10/12/12
to shar...@googlegroups.com
Sounds cool, please add the following attribute:

   [JsType(JsMode.Prototype)]
    public static class Extensions
    {

        [JsMethod(ExtensionImplementedInInstance=true)]

        public static jQuery each<T>(this jQuery j, JsAction<JsNumber, T> function)
        {
            return j.each(function);
        }
}

adriana...@gmail.com

unread,
Oct 12, 2012, 5:48:22 PM10/12/12
to shar...@googlegroups.com
Perfect!

Thanks.

adriana...@gmail.com

unread,
Oct 12, 2012, 5:52:53 PM10/12/12
to shar...@googlegroups.com, adriana...@gmail.com
I noticed that it generates the following:

SharpKit.jQuery.Extensions.each = function(j,function)
{
    return j.each(function);
};

Which is awesome, but do we really need the extra stack call?
Can I achieve the same with creating only a stub so it doesn't really creates a new function but uses the existing one?

Thanks,
Adrian

Dan-el Khen

unread,
Oct 12, 2012, 5:54:29 PM10/12/12
to shar...@googlegroups.com, adriana...@gmail.com
This method doesn't get called at all. You should set your class to this:
[JsType(JsMode.Prototype, Export=false)]

Dan-el

Adrian Aisemberg

unread,
Oct 12, 2012, 5:59:22 PM10/12/12
to Dan-el Khen, shar...@googlegroups.com
Now it's even more perfect!
Thanks a lot.

Dan-el Khen

unread,
Oct 13, 2012, 12:02:57 PM10/13/12
to Adrian Aisemberg, shar...@googlegroups.com
No prob... :-)

Dan-el

pushpendra...@gmail.com

unread,
Apr 26, 2013, 9:47:28 AM4/26/13
to shar...@googlegroups.com, adriana...@gmail.com
My search function using jquery does not take property in function:
        $.each(items, function (index) {
        var properyToMatch = 'value';
          ////console.log("items is setJqxComboboxSelectedItem global fun... "+this.properyToMatch+"and item to select :"+itemToSelect);
            if (this.properyToMatch === itemToSelect) {
               ////console.log("items is setJqxComboboxSelectedItem global fun... "+this.properyToMatch+"and item to select :"+itemToSelect);

                indexToSelect = index;
                return false;
            }
        });
  I shows this.properyToMatch as undefined.



Please help me.
Thanks in advance.
Regards,
Pushpendra

Dan-el Khen

unread,
Apr 26, 2013, 10:02:24 AM4/26/13
to shar...@googlegroups.com, Adrian Aisemberg
Hi,

Can you explain what you're trying to do? Because I can't quite figure it out from your sample. All I see is that you declared a variable named propertyToMatch, but then you tried accessing it using the 'this.' prefix, which of course will be undefined. One more question, are you trying to do this using SharpKit? If so, can you post your C# code with it?

Cheers
Dan-el


--
You received this message because you are subscribed to the Google Groups "SharpKit Support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sharpkit+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

pushpendra kumar

unread,
Apr 27, 2013, 5:44:32 AM4/27/13
to shar...@googlegroups.com
HI Dan-el,

            Actually i want to search the elements on the basis of different properties dynamically. Like value,label,group etc.
So in the var propertyToMatch i have value,label string and then search in array on the basis of that.
Also i am not using sharpkit and have no idea of same.

Regards,
Pushpendra


--
You received this message because you are subscribed to a topic in the Google Groups "SharpKit Support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sharpkit/9Kmwn5vRsPE/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to sharpkit+u...@googlegroups.com.

Dan-el Khen

unread,
Apr 28, 2013, 5:59:12 AM4/28/13
to shar...@googlegroups.com
Hi, 

Ok, what you need to do is to either use JavaScript attribute access using brackets: obj["prop"], or use create a new jQuery object for each element and use the jQuery attr("attributeName") / prop("propertyName") method:

var properyToMatch = "value";
var q = $("A");
var valueToMatch = "Hello";


q.each(function (index, el) {
//method1
if(el[propertyToMatch]==valueToMatch)
     console.log("Found!");

//method2:
var q2 = $(el);
if(q2.attr(propertyToMatch)==valueToMatch)
     console.log("Found!");
 }

You can also do this with SharpKit, and write this code in C#, and get a better compile-time validation on your code, also to manage a large code base more easily.

Hope this answers your question, if you need any further help with this feel free to post.

Cheers
Dan-el

pushpendra kumar

unread,
May 4, 2013, 10:19:43 PM5/4/13
to shar...@googlegroups.com
Thank you so much Dan-el Khen.

Can you explain please, how can use sharpkit code in C# with javascript.

Thanks and Regards,
Pushpendra

Dan-el Khen

unread,
May 7, 2013, 2:48:36 AM5/7/13
to shar...@googlegroups.com
Well, the basic concept is that it's hard maintaining a complex / large JavaScript project in pure JavaScript since you can't compile / refactor / cleanup / modify your code. SharpKit allows you to generate the exact same code, but maintain the code in C#, thus gaining all of your IDE's and tools features.

Cheers
Dan-el

pushpendra kumar

unread,
May 7, 2013, 3:50:56 AM5/7/13
to shar...@googlegroups.com
Thank you so much Dan,
  
     Its working properly.

Regards,
Pushpendra

Dan-el Khen

unread,
May 7, 2013, 1:03:43 PM5/7/13
to shar...@googlegroups.com
No problem :-)
Cheers

pushpendra kumar

unread,
May 8, 2013, 1:24:49 AM5/8/13
to shar...@googlegroups.com
Hi Dan,

          Can you please explain the career scope in backbone. js.
          As i am working on backboneJs with requireJs these days.
          And want to make my career as javascript developer with backbone.js.


Regards,
Pushpendra

Dan-el Khen

unread,
May 12, 2013, 12:17:52 PM5/12/13
to shar...@googlegroups.com
Hi,

I'm not sure how to answer this, but the general idea is that you can use SharpKit to generate any type of code, as well as to use any library. While it's allowing you to maintain your code in C#, which have many advantages over writing code directly in JavaScript.

Cheers
Dan-el
Reply all
Reply to author
Forward
0 new messages