Announcement

Collapse
No announcement yet.

Inline dynamic types

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    Inline dynamic types

    Is there a possibility to skip the dynamic type class generation in favor of simple types for e.g. option objects commonly used in js libraries?

    I frequentyly use this to call external js:
    Script.Get<dynamic>("$('body')").css(new { opacity = 0 }).animate(new { opacity = 1 }, 1000, "swing");
    What I want generated is this:
    $('body').css({ opacity: 0 }).animate({ opacity: 1 }, 1000, 'swing');
    Insead what I get generated is an unneccessary dynamic type definition that costs bandwidth and in this case does not even work with jQuery animations:
    $('body').css(new $asm.$AnonymousType$1(0)).animate(new $asm.$AnonymousType$1(1), 1000, "swing");
    
        Bridge.define("$AnonymousType$1", $asm, {
            $kind: "anonymous",
            ctors: {
                ctor: function (opacity) {
                    this.opacity = opacity;
                }
            },
            methods: {
                equals: function (o) {
                    if (!Bridge.is(o, $asm.$AnonymousType$1)) {
                        return false;
                    }
                    return Bridge.equals(this.opacity, o.opacity);
                },
                getHashCode: function () {
                    var h = Bridge.addHash([7550196186, this.opacity]);
                    return h;
                },
                toJSON: function () {
                    return {
                        opacity : this.opacity
                    };
                }
            },
            statics : {
                methods: {
                    $metadata : function () { return {"m":[{"a":2,"n":"opacity","t":16,"rt":System.Int32,"g":{"a":2,"n":"get_opacity","t":8,"rt":System.Int32,"fg":"opacity"},"fn":"opacity"}]}; }
                }
            }
        });
    Is there any way to simplify all of this?

    Example: https://deck.net/6b582392f4cfec67c6741c63d55eb4b2/top

    #2
    If I wrap dynamic objects in Script.ToObjectLiteral<dynamic>(), I get what I want:
    Script.Get<dynamic>("$('body')")
        .css(Script.ToObjectLiteral(new { opacity = 0 }))
        .animate(Script.ToObjectLiteral(new { opacity = 1 }), 1000, "swing");
    Generates what I want and without any dynamic type class definitions:
    $('body').css({ opacity: 0 }).animate({ opacity: 1 }, 1000, "swing");
    Now I only need to somehow get rid of the Script.ToObjectLiteral() clutter, to make the code readable again.

    Updated example: https://deck.net/19d32fe791322fa66ec35923b6e8b5e9/top

    Comment


      #3
      Hi,

      You can use dynamic object
      https://deck.net/809d0540352b23619f02ac71bb5291af

      Comment


        #4
        Thanks for your suggestion. Unfortunately this introduces local variable definitions and generates more code than necessary.

        dynamic cssOptions = new object();
        cssOptions.opacity = 0;
        dynamic animateOptions = new object();
        animateOptions.opacity = 1;
        Script.Get<dynamic>("$('body')").css(cssOptions).animate(animateOptions, 1000, "swing");
        Now this is generated with dynamic objects:

        var cssOptions = {  };
        cssOptions.opacity = 0;
        var animateOptions = {  };
        animateOptions.opacity = 1;
        $('body').css(cssOptions).animate(animateOptions, 1000, "swing");
        I think I still prefer the ToObjectLiteral() method, because the generated code is minimal and I don't need to declare local variables.

        Updated example: https://deck.net/ae12ae0bbeff89195309211146b79d36/top

        Comment

        Working...
        X