Archive for the ‘Javascript’ Category

Quick Tip: An Introduction to Sammy.js

Miércoles, Agosto 11th, 2010

You’ve been seeing it for a while now with Google’s Reader, Gmail, and most recently, on Facebook. Probably, you, too, would like to write RESTful evented JavaScript applications. Well, fellow developers, meet Sammy.js, a tiny JavaScript framework built on top of jQuery. Sammy utilizes the URL hash (#) to allow you to create single page AJAX applications that respond to your browser’s back button. Interested?

In this article, I’ll be providing a short overview of the framework, and then a brief demonstration of what it’s like working with Sammy.js, with the hope of enticing you enough to consider it for your projects.


Setting the Stage

“Sammy.js is light both in size (<20kb) and footprint. Pull it into your already started applications.”

Sammy.js is being put together by Aaron Quint, a web developer out of Brooklyn, NY. Its API is modeled on the popular ruby framework, Sinatra, and is great for both simple and complex applications. It’s easy to get into, and can be pulled into your existing projects. It’s not an all or nothing proposition; so let’s take a look.

Sammy.js allows you to write single page apps, much like Gmail. You can maintain the state of your app with the url, without having to refresh or change the page. There are other MVC JavaScript frameworks, like SproutCore, which tend to be all encompassing. But with Sammy, you have a light (~20kb) framework, capable of invoking several instances simultaneously (ie. running multiple apps in the same document).


Opening Act

Installing Sammy.js is pretty straightforward. Head on over to the download page, grab a copy and move, sammy-0.5.4.min.js to where you store your project’s libraries (typically /js for me). For the purpose of this article, I will be using version 0.5.4, but you may be inclined to try sammy-latest.min.js. You’ll also need a copy of jQuery, at least v. 1.4.1. As with most jQuery plugins, order is important: jQuery, before Sammy.js, before your JavaScript. I tend to put my JavaScript at the bottom of the page, because it blocks other items from loading in parallel, giving the impression of a slower loading page. So far we have:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    	<title>Sammy.js Example</title>
    </head>
    <body>

        <div id="content"></div>

        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
        <script type="text/javascript" src="js/sammy-0.5.4.min.js"></script>
        <script type="text/javascript">

            // your script goes here

        </script>

    </body>
    </html>

Now to start coding our app. To keep things simple, I’m working inline, which isn’t the best practice. Adding a Sammy.js application is as simple as assigning it to a variable, which I’m calling ratPack. On our page we’ve defined a div with the id “content” upon which our application will be acting. We indicate this as follows:


    var ratPack = $.sammy(function() {

        this.element_selector = '#content';

        // routes will go here

    });

The importance of the element selector is that we can have multiple instances of Sammy.js running in our document, affecting different elements.


Main Attraction

Sammy.js uses the path, as defined in the URL hash, and the common HTTP methods (get, post, put, delete) to determine a callback function to invoke. These are typically known as “routes”. Some examples from around the web would be:

As an example, we’ll sketch up a mailbox application. Let’s first setup the default route of our app, which will land on #/inbox.

    this.get('#/inbox', function(context) {
        context.app.swap('');
        context.$element().append('<h1>inbox</h1>');
    });

Here, you can make the callback function do whatever you’d like. Since I’m displaying an inbox, I’d probably want to make an ajax call and retrieve a list of messages. However, for the sake of simplicity, I’m just going to return a h1 tag. The context.app.swap('') tells Sammy to replace what’s in my content div, rather than just appending to it.

In order to get this working in the browser, we’ll want to run the app using jQuery’s document ready function and pass it to our starting URL, as defined in the above route.


    $(function() {
      ratPack.run('#/inbox');
    });

And that’s it. We should now be able to load our document in a browser, the app should launch and navigate us to our inbox.

Next, we can create another route to handle labeled messages:

    this.get('#/label/:name', function(context) {
        context.app.swap('');
        context.$element().append('<h1>' + this.params['name'] + '</h1>');
    });

Sammy uses the colon var syntax (:var) to return parameters for us to filter our messages. Again, I’m just displaying the name of the label.

To this point, we’ve only been using the “get” HTTP method. Say we were to create a form and route it to #/compose:

    this.get('#/compose', function(context) {
        context.app.swap('');
        context.$element().append('<h1>say hello to?</h1>'
          + '<form action="#/compose" method="post">'
          + '<input type="text" name="to" />'
          + '<input type="submit" name="submit" />'
          + '</form>');
    });

Now we can setup a route to accept the posted data and have Sammy parse it for us.

this.post('#/compose', function(context) {
    context.app.swap('');
    var to = this.params['to'];
    context.$element().append('<h1>hi ' + to + '</h1>');
});

That’s the basics. A simple API, powerful enough for projects both big and small. If you are following along with the code, we can add some navigation for ease of clicking.

    <div id="nav">
        <ul>
            <li><a href="#/inbox">inbox</a></li>
            <li><a href="#/compose">compose</a></li>
            <li><a href="#/label/drafts">drafts</a></li>
        </ul>
    </div>

Encore

Of course, Sammy.js has a lot more to offer than simply defining routes in the app. More advanced users can explore custom events and namespaces, for event-driven applications. Aaron is trying to keep the core of Sammy.js as tight as possible, but it also comes with a lot of plugins. There’s a title plugin, that allows you to easily set the document’s title for different routes. There are several templating systems, including haml and mustache. There’s a nice-looking form builder and Sammy.Store, “an abstract adapter class that wraps the multitude of in browser data storage into a single common set of methods for storing and retreiving data.”

I hope you’ve enjoyed this quick look at Sammy.js and are ready to consider using it in your applications. Let’s hear your thoughts on Sammy.js in the comments!

Lenguajes de programación emergentes (1a. parte)

Lunes, Julio 26th, 2010

Los lenguajes de programación son mi fascinación. No sólo se trata de programar/codificar con ellos. Se trata también de conocer los detalles de las diferentes sintaxis, así como la expresividad que tiene cada uno para resolver problemas de todo tipo. Cada uno, como sucede con cualquier idioma hablado, es una forma de ver y entender el mundo.

En lo que sigue te presento la primera parte de dos sobre 10 lenguajes de programación emergentes. Todos son libres/abiertos. Todos son raros, únicos de alguna manera —allí comienza mi curiosidad—. Casi todos tienen menos de 10 años de vida. Los hay orientados a objetos, funcionales, orientados a pila y más. Y no es que debas probarlos todos, pero sí te invito echarles un vistazo con el mismo espíritu con el que un arqueólogo descubre lenguajes inimaginables. En todos los casos presento una breve ficha técnica, así como un ejemplo: no el clásico “Hello, world!”, sino un pequeño programa para listar los números del 1 a 10. Da clic en los títulos para ir a la página oficial del lenguaje en cuestión.

1. Io

  • Año de creación: 2002.
  • Paradigma: Orientado a objetos, basado en prototipos (como en JavaScript).
  • Descripción: Simple, muy dinámico, introspectivo y concurrente. Multiplataforma y extensible. Fue diseñado para unir la expresividad de Smalltalk con la velocidad de C.
  • Inspirado en: Smalltalk (principalmente), Lisp, Lua, Self.

  • Instalación: En Ubuntu:

    1. Descarga
    2. Descomprime
    3. Ingresa al directorio
    4. mkdir build && cd build
    5. cmake ..
    6. sudo make install
    7. ldconfig
  • Ejemplo:

      for ( i, 1, 10, write(i, " ") )
    

2. Clojure

  • Año de creación: 2007.
  • Paradigma: Funcional, pero de propósito general.
  • Descripción: Compilado, eficiente, robusto, multihilado, con sistema de macros. Corre sobre dos máquinas virtuales: JVM y CLR.
  • Inspirado en: Lisp, Haskell.
  • Instalación:

    1. En Ubuntu: sudo apt-get install clojure.
    2. De otra forma, descarga el código; entonces compila e instala con ant.
  • Ejemplo:

      (dotimes [i 10] (print (inc i) ""))
    

3. D

  • Año de creación: 1999.
  • Paradigma: Multiparadigma, pero sobre todo orientado a objetos e imperativo.
  • Descripción: Es muy parecido a C/C++, pero intenta ser mucho más práctico. Gdc y ldc son compiladores libres para el lenguaje D.
  • Inspirado en: C++, Java.
  • Instalación:

    1. En Ubuntu: sudo apt-get install gdc
    2. De otra forma, descarga el código oficial, descomprime y entra a ./dmd/linux/bin.
  • Ejemplo:

      import std.stdio;
    
      int main() 
      {
                for (int i = 1; i <= 10; i++)
                            writef(i, " ");
                 return 0;
      }
    

4. Go

  • Año de creación: 2009.
  • Paradigma: Imperativo y concurrente.
  • Descripción: Es como C, pero sin declaración de tipos de datos como en Python. Su sintaxis es sencilla. La compilación es, sin exagerar, excepcionalmente rápida. Ken Thompson, también co-creador de C, ayudó en el diseño.
  • Inspirado en: C, Pascal.
  • Instalación: Cambia el valor de las variables según tu conveniencia. En Ubuntu:

      export GOROOT=$HOME/gosource
      export GOARCH=386
      export GOOS=linux
      export GOBIN=$HOME/gobin
      export PATH=$PATH:$GOBIN
    
      source ~/.bashrc
    
      sudo apt-get install bison gcc libc6-dev ed gawk make 
      sudo apt-get install python-setuptools python-dev build-essential
      sudo easy_install mercurial
    
      hg clone -r release https://go.googlecode.com/hg/ $GOROOT
    
      cd $GOROOT/src
      ./all.bash
    
  • Ejemplo:

      package main
      import "fmt"
    
      func main() {
    
         for i:=1; i<=10; i++ { 
            fmt.Printf("%d ", i)
         }
    
      }
    

5. Factor

  • Año de creación: 2003.
  • Paradigma: Orientado a pila, concatenativo (sobre todo) y funcional.
  • Descripción: Definición dinámica de los tipos de datos, manejo automático de memoria, compilador o, si se quiere, también interactivo.
  • Inspirado en: Forth, Self.
  • Instalación: En Ubuntu:

    1. Descarga
    2. Descomprime
    3. Ingresa al directorio
    4. ./factor
  • Ejemplo: (Sé que se puede hacer mejor. Ya aprenderé.)

      { 1 2 3 4 5 6 7 8 9 10} [ "" write . ]  each
    

Lenguajes de programación emergentes (1a. parte) escrita en Bitelia el 26 July, 2010 por alan.lazalde
Enviar a Twitter | Compartir en Facebook

Consola de Javascript sucia y rápida

Viernes, Agosto 8th, 2008

Ya se que existe firebug en todas su variantes, pero a veces es necesario un parchecito rapidito para los navegadores que no lo utilicen, o simplemente porque es divertido.

El código javascript:

function ejecutarJS(strSentencia, idResultado){
			var resultado = eval(strSentencia);
			document.getElementById(idResultado).innerHTML = 
			document.getElementById(idResultado).innerHTML + resultado;
		}

HTML:

<div id="boxConsola">
		<div id="jsConsola">
		<p>
			<textarea id="jsConsulta" ></textarea>
		</p>
		<p>
			<input type="button" value="Ejecutar" onClick="ejecutarJS(document.getElementById('jsConsulta').value, 'jsResultado');" />
			<input type="button" value="Borrar" onClick="document.getElementById('jsConsulta').innerHTML = '' ; document.getElementById('jsResultado').value = '' ;"
		</p>
	</div>
 
	<div id="jsResultado">
	</div>
</div>

Y finalmente, el CSS Mágico:

#boxConsola {
	position:absolute;
	bottom:0px;
	padding: 10px;
	right:0px;
	width: 660px;
	border: 1px solid #ABB0B4;
}
	/* estas cosas que hay que hacer por IE... */
	body > div#boxConsola {
		position: fixed;
	} 
 
	#jsConsola {
		width: 205px;
		float: left;
	}			
 
		#jsConsulta {
			width: 200px;
			height: 100px;
		}
 
	#jsResultado {
		width: 452px;
		height: 120px;
		float: right;
		border: 1px solid #ABB0B4;
		background-color: #EDEDED;
	}

Obviamente, como práctico, nada. Y el demo se los debo, poner una consola de javascript en un servidor es como poner un cartel invitando a hacer maldades :P .

Mootools 1.2 lanzado

Martes, Junio 10th, 2008

Finalmente, luego de luchar contra betas bastante inestables, se ha liberado la version 1.2(ahora si, bien solida) de este ya popular (y excelente) framework js.