Archive for February, 2013

Javascript Promise Pattern

Posted: February 11, 2013 in javascript
Tags:

One of the things i usually talk about when referring to JavaScript programming is design and implementation pattern.

From all the JavaScript patterns out there one had been over spotlights lately because is very popular among Windows 8 HTML5 developer’s. One thing that many people don’t know is that the WinJS Windows 8 Promises are not more than an implementation of a JavaScript design pattern.

Promise Pattern can be very useful when programming in JavaScript  even if you are using JavaScript on the web. Basically a promise is an object that represents a future result (that’s way in many books some authors call them future), that will be obtained in a near future.

The promise pattern offer’s a way to use callbacks in a friendly and readable way. We can also see Promises as a more elegant way to do callbacks in JavaScript.

Let’s see an example:

operation1(params1) {

  async_task(params1, function() {
    operation2(params2) {
               async_task(params2, function() {
                   //do something
              }
          }
     }
}
If you look carefully to the example you will see that you have a async chain call stack, this is a dummy example but if you imagine in some real scenarios this can become truly confuse, especially if you have some future need of extend the code. When thinking in an implementation based on Promise we are talking in a much clean way to solve the problem, next you have the pseudo code who solves the same problem we previously had.

function operation1(params1) {

  var promise = new Promise()
  async_task(params1, function() {
    // ...
    promise.resolve(params2);
  });
  return promise;
}
function operation2(params2) {
  var promise = new Promise(this)
  async_task(params2, function() {
    // do something
  });
  return promise;
}

operation1(params1).then(operation2);

 

The Promise pattern allow us to have a representation of a long runing process, instead of blocking and waiting for never ending loops. You can see the application of this pattern to JQwuery and other’s in http://blogs.msdn.com/b/ie/archive/2011/09/11/asynchronous-programming-in-javascript-with-promises.aspx

 

HTML 5 Basic

Posted: February 3, 2013 in HTML 5
Tags:

Last week a friend of my that is Web Programming teacher in the University, told me that in is discipline this semester he have to teach HTML 5 and in the first classes we had a lot of difficult to explain to some of the students who are web developer’s the why of HTML5 new tags. When we talk about semantic the students start asking about microdata and all they care about hmtl 5 is the death of flash.

This reminds me some discussions i had in the past about HTML and the need to learn and change to HTML 5, so i decided to wirte this post for the ones who are learning/teaching or needing to understand the why of  HTML 5. This post is also a “resume” of the way i typically use to teach HTML 5 to developer’s or to explain to some clients what’s HTML 5.

Not a long time ago if we make a big search in the HTML development almost all the HTML development on the web follow the following struture, so this structure is not strange.

html5 inspiration

Almost everyone was using this structured in their development, this was made for different reasons like the need to have a structure compatibly with some of the JavaScript platforms or CSS framework, or the need to transmit semantic to different system’s developer’s or languages. So if semantic is a issue and a lot of people are using this pattern in a useful way, why not to use each decoration of this tags as first citizens as tags. In response to that the work group that was working in HTML had understand the need to create new semantic tags to maintain structured the HTML language. And the structure had evaluated to:

html5tags

 

This are just some of the new tags and many times only this painting is not enough to explain what is the role played by each one of this elements, so we had made a grid to help you:

semantic htl5

 

 

I hope this post can help you to have a better understanding of what are the reason who had take W3C to normalize this new tags, why the name is that and what is the reason for his creation.

HTML 5 brings a lot of other great things that deserve a look like Forms , Video or Canvas, their are other advanced ways to specefy semantic  that worth some of your time study.