case
28Sep/100

Parse XML in JavaScript with jQuery

Yes, you can do it with XMLDOM and all the other ways out there, but why not use the power of jQuery ?
Like so:

1
2
3
4
5
6
7
   <root>
      <items>
          <item name="first" id="1">Content 1</item>
          <item name="second" id="2">Content 2</item>
      </items>
      <other atribute="true" />
    </root>
1
2
3
4
   var xml = '<root ......';
   var obj = $(xml);
   // and now you can use jquery selectors to get what you want
   $("item[name=second]", obj).attr('id'); // 2

6Sep/100

JavaScript Object-Oriented Programming (OOP) – jClass

I just finished working on a JavaScript 'framework' that will allow you to write highly object-oriented code in your browser.
Using jClass you can easily define namespaces, create and extend class, define and implement interfaces, define public/private/static methods, override and overload them, have getters and setters, and so on.

The syntax for writing code using jClass looks something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
include("some.other.files");
 
namespace("my.namespace")
({
    MyClass : jClass.extends(BaseClass).implements(SomeInterf, SomeOtherInterf)
    ({
        constructor : function(){
         ....
        },
        private : {
          ....
        },
        public : {
          ....
        },
        static : {
          ....
        }
    }),
    OtherClass : jClass({
       ......
    })
})

I'm also working on some useful libraries that you can use with jClass.
Usage examples, documentation and downloads are all available on Google Code right HERE

11Aug/106

On typing finished jQuery plugin

If you are developing a form and you require certain validations; check if username is available or not for example - the way to do that is to listen for the change/focusout DOM events. You can also check it on every keypress but if the validation does some expensive checking in the background that is out of the question. So here is a really small jQuery plugin that does tell you when a user finished typing inside a text box. The way it does it is to compute the speed the user is typing with and when a delay longer than twice the speed average occurs, the event is triggered and your validation function is called. Simple enough.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$(document).ready(function () {
  $.fn.onTypeFinished = function(func) {
     var T = undefined, S = 0, D = 1000;
     $(this).bind("keypress", onKeyPress).bind("focusout", onTimeOut);
     function onKeyPress() {
        clearTimeout(T);
        if (S == 0) { S = new Date().getTime(); D = 1000; T = setTimeout(onTimeOut, 1000); return; }
        var t = new Date().getTime();
        D = (D + (t - S)) / 2; S = t; T = setTimeout(onTimeOut, D * 2);
     }
 
      function onTimeOut() {
           func.apply(); S = 0;
      }
      return this;
   };
});

The way to use it:

1
$("input[name='username']").onTypeFinished(myValidationFunction)

And here is a small demo:

23Jun/102

How to run python code in Windows batch (bat) files

Here’s the bit of tricky batch file magic that does it:

1
2
3
@setlocal enabledelayedexpansion && python -x "%~f0" %* & exit /b !ERRORLEVEL!
#start python code here
print "hello world"

The way it works is that the first line of the file does two different things.

1. starts python interpreter passing the name of the file in, and the -x option will tell it to skip the first line (containing .bat file code)
2. When python finishes the script exits.

This nifty trick makes it much nicer for writing admin scripts with python on Windows.

21May/105

FlashBuilder network issue

girl scream 150x150 FlashBuilder network issueLike most of my articles here, I try to post solutions for unusual problems I encounter now and then that may help someone someday. And this one almost made me scream.

So here's the story. My DEV environment (http://dev/) is a Virtual Host mapped in the Windows host file to point to a sandboxed directory somewhere on my hard drive. The Flex project I'm building uses a RemoteObject and tries to communicate with a AMFPHP gateway to do it's stuff. All good till here.

The weird part is that when I try to run the app, even though my flash is coming from the DEV host and the path to the gateway points to the same DEV host, the AMF calls either failed with a security exception or if they worked, they were extremely slow. The delay between a service call and a response/error was somewhere between 6 and 10 seconds.

Why oh why !? The AMFPHP Browser app that comes with the package works just fine so my app was at fault somehow. Tried some investigations and thanks to Firebug I found the vital clue.

It seems that my app was trying to make the connection to the gateway through http://localhost:37813/. WTF ?? I didn't had any reference to 'localhost' in my code, yet the app was trying to connect using that address for some reason. And quite often, the crossdomain.xml failed to load resulting in a security error.

So what in the world is the problem then ? Well, Flash Builder seems to be the problem (and me for not RTFM).

It seems that if you compile your swf with Network Monitor active, which you may not realize, especially if the window is not even opened, you have just told your swf to redirect all traffic to localhost:37813. It will compile the redirect info into your flash. Traffic may work if you have Flash Builder debugger running but try to deploy that somewhere. Heh.

All you have to do is turn network monitor off. Recompile your swf. Redeploy.

Ta da !