Script Sharp - desarrolla en C Sharp y genera código Javascript

Script# (Script Sharp), es un proyecto de Nikhil Kothari (creador del web development helper), básicamente es una herramienta para que los desarrolladores (.NET) que escriben código C# (C sharp) no tengan que escribir código javascript. Cabe resaltar que esta herramienta ha obtenido un premio a la excelencia en la ingenieria que otorga Microsoft.

Una imagen de como es la herramienta:

Y aqui va el tipico ejemplo “Hola Mundo”:

Si tenemos estos elementos en nuestro documento HTML:


<input type="text" id="nameTextBox" />
<input type="button" id="okButton" value="OK" />
<span id="helloLabel"></span>

Ahora escribo el script en C sharp en mi fichero HelloWorld.cs, haremos lo siguiente: en el evento onclick del boton (input type=”button”) recupero el valor de la caja de texto (nput type=”text”) y luego hare una llamada ajax y finalmente mostraré el resultado en la etiqueta (span).


using System;
using ScriptFX;
using ScriptFX.UI;

namespace HelloWorld {

    public class HelloWorldScriptlet : IScriptlet {

        private Button _okButton;
        private TextBox _nameTextBox;
        private Label _helloLabel;

        private XMLHttpRequest _request;

        public void Start() {
            _okButton = new Button(Document.GetElementById("okButton"));
            _nameTextBox = new TextBox(Document.GetElementById("nameTextBox"));
            _helloLabel = new Label(Document.GetElementById("helloLabel"));

            _okButton.Click += new EventHandler(OnOKButtonClick);
        }

        private void OnOKButtonClick(object sender, EventArgs e) {
            Callback completedCallback = new Callback(this.OnRequestComplete);

            _request = new XMLHttpRequest();
            _request.Onreadystatechange = Delegate.Unwrap(completedCallback);
            _request.Open("GET", "Hello.axd?name=" + _nameTextBox.Text, /* async */ true);
            _request.Send(null);
        }

        private void OnRequestComplete() {
            if (_request.ReadyState == 4) {
                _request.Onreadystatechange = null;

                string greeting = _request.ResponseText;
                _helloLabel.Text = greeting;
            }
        }
    }
}

Segun Nikhil aplicamos la magia del Script Sharp, con el siguiente comando:

ssc /ref:sscorlib.dll /ref:Script.ScriptFX.Core.dll /debug /out:HelloWorld.js HelloWorld.cs

y luego tenemos nuestro fichero HelloWorld.js con el siguiente código javascript:


Type.registerNamespace('HelloWorld');

////////////////////////////////////////////////////////////////////////////////
// HelloWorld.HelloWorldScriptlet

HelloWorld.HelloWorldScriptlet = function Scenarios_HelloWorldScriptlet() {
}
HelloWorld.HelloWorldScriptlet.prototype = {
    _okButton: null,
    _nameTextBox: null,
    _helloLabel: null,
    _request: null,

    start: function Scenarios_HelloWorldScriptlet$start() {
        this._okButton = new ScriptFX.UI.Button(document.getElementById('okButton'));
        this._nameTextBox = new ScriptFX.UI.TextBox(document.getElementById('nameTextBox'));
        this._helloLabel = new ScriptFX.UI.Label(document.getElementById('helloLabel'));
        this._okButton.add_click(new Delegate(this, this._onOKButtonClick));
    },

    _onOKButtonClick: function Scenarios_HelloWorldScriptlet$_onOKButtonClick(sender, e) {
        var completedCallback = new Delegate(this, this._onRequestComplete);
        this._request = new XMLHttpRequest();
        this._request.onreadystatechange = Delegate.unwrap(completedCallback);
        this._request.open('GET', 'Hello.axd?name=' + this._nameTextBox.get_text(), true);
        this._request.send(null);
    },

    _onRequestComplete: function Scenarios_HelloWorldScriptlet$_onRequestComplete() {
        if (this._request.readyState == 4) {
            this._request.onreadystatechange = null;
            var greeting = this._request.responseText;
            this._helloLabel.set_text(greeting);
        }
    }
}

HelloWorld.HelloWorldScriptlet.registerClass('HelloWorld.HelloWorldScriptlet', null, ScriptFX.IScriptlet);

Página Oficial:
http://www.nikhilk.net/ScriptSharpIntro.aspx

Video (10 minutos):
http://www.nikhilk.net/Content/Video/ScriptSharpIntro.wmv

Descargar:
http://projects.nikhilk.net/Binaries/ScriptSharp.zip

Tutorial PDF:
http://projects.nikhilk.net/Binaries/ScriptSharp.pdf

Sin comentarios

No comments yet

Leave a reply